package com.ruoyi.system.service.impl;


import cn.hutool.core.date.DateTime;
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.Hit;
import co.elastic.clients.elasticsearch.indices.GetIndexResponse;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.quartz.util.QuartzJobExecution;
import com.ruoyi.system.constants.CommonConstants;
import com.ruoyi.system.domain.ByDb15;
import com.ruoyi.system.domain.ByExportTask;
import com.ruoyi.system.domain.ByHistoryFile;
import com.ruoyi.system.domain.StandardDb15W;
import com.ruoyi.system.es.ESBuilder;
import com.ruoyi.system.es.ESContext;
import com.ruoyi.system.service.*;
import com.ruoyi.system.task.ByExportThreadFileTask;
import com.ruoyi.system.task.ByExportThreadTask;
import io.lettuce.core.StrAlgoArgs;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.entity.ContentType;
import org.aspectj.weaver.loadtime.Aj;
import org.bouncycastle.jcajce.provider.asymmetric.ec.KeyFactorySpi;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * es业务处理层
 *
 * @author ruoyi
 * @date 2024-03-12
 */
@Slf4j
@Service
public class EsServiceImpl implements IEsService {


    @Autowired
    private ESContext esContext;
    @Autowired
    private IByDb15Service iByDb15Service;
    @Autowired
    private IStandardDb15WService iStandardDb15WService;
    @Autowired
    private IStandardFileService iStandardFileService;
    @Autowired
    private IByExportTaskService byExportTaskService;
    @Autowired
    private IByHistoryFileService iByHistoryFileService;

    /** oa所需命名控件参数 */
    @Value("${newFilePath}")
    private String newFilePath;

    /** oa所需命名控件参数 */
    @Value("${es.ip}")
    private String eshost;

    /** oa所需命名控件参数 */
    @Value("${es.port}")
    private String seport;

    @Autowired
    @Qualifier("exportThreadPoolTaskExecutor")
    private ThreadPoolTaskExecutor taskExecutor;

    @Override
    public AjaxResult createIndex() {
        boolean flag = false;
        try {
            ESBuilder standard = ESBuilder.builder().build();
            //默认es字段
            standard.buildString("id")
                    .buildString("a000")
                    .buildString("a001")
                    .buildString("a003")
                    .buildString("a010")
                    .buildString("a011")
                    //标准号
                    .buildString("a100")
                    //发布日期
                    .buildString("a101")
                    //发布机构
                    .buildString("a102")
                    .buildString("a103")
                    .buildString("a104")
                    //标准状态
                    .buildString("a200")
                    .buildString("a203")
                    .buildString("a204")
                    //实施或试行日期
                    .buildString("a205")
                    //废止日期
                    .buildString("a206")
                    .buildString("a207")
                    .buildString("a208")
                    //起草单位
                    .buildString("a209")
                    .buildString("a210")
                    //中文名称
                    .buildString("a298")
                    .buildString("a300")
                    .buildString("a301")
                    //英文标准名称
                    .buildString("a302")
                    .buildString("a304")
                    .buildString("a305")
                    .buildString("a309")
                    .buildString("a310")
                    .buildString("a311")
                    //中文文摘
                    .buildString("a330")
                    //英文文摘
                    .buildString("a331")
                    .buildString("a390")
                    .buildString("a409")
                    .buildString("a448")
                    //代替标准
                    .buildString("a461")
                    //被代替标准
                    .buildString("a462")
                    .buildString("a502")
                    .buildString("a505")
                    .buildString("a800")
                    .buildString("a805")
                    .buildString("a807")
                    .buildString("a808")
                    .buildString("a820")
                    .buildString("a821")
                    .buildString("a823")
                    .buildString("a824")
                    //中国标准分类号
                    .buildString("a825")
                    //国际标准分类号
                    .buildString("a826")
                    .buildString("a835")
                    .buildString("a840")
                    .buildString("a837")
                    .buildString("a845")
                    .buildString("a846")
                    .buildString("a847")
                    .buildString("a850")
                    .buildString("a860")
                    .buildString("a861")
                    .buildString("a863")
                    .buildString("a864")
                    .buildString("a865")
                    .buildString("a866")
                    .buildString("a870")
                    .buildString("a871")
                    .buildString("a885")
                    .buildString("a886")
                    .buildString("a890")
                    .buildString("a891")
                    .buildString("a892")
                    .buildString("a893")
                    .buildString("a894")
                    .buildString("a895")
                    .buildDate("create_time", "yyyy-MM-dd HH:mm:ss")
                    .buildString("textAddress")
                    .buildString("filePath")
                    .buildString("reserve_a")
                    .buildString("reserve_b")
                    .buildString("reserve_c")
                    .buildString("reserve_d")
                    .buildString("reserve_e")
                    .buildString("table_name");
            flag = esContext.createIndex(CommonConstants.INDEX_PREFIX, standard.getMapping());
            log.info("创建索引成功");
        }catch (Exception e){
            flag = false;
            log.info("创建索引失败:{}", e.getMessage());
        }
        return null;
    }

    @Override
    public AjaxResult createStructIndex() {
        boolean flag = false;
        try {
            ESBuilder standard = ESBuilder.builder().build();
            //默认es字段
            standard.buildString("id")
                    .buildString("type")
                    .buildString("clg_id")
                    .buildString("clg_name")
                    .buildString("n_docum")
                    .buildString("app_id")
                    .buildString("app_body")
                    .buildString("ref_item")
                    .buildString("t_id")
                    .buildString("t_cn")
                    .buildString("t_en")
                    .buildString("t_def")
                    .buildString("t_note")
                    .buildString("t_exp")
                    .buildString("pic")
                    .buildString("tech_itid")
                    .buildString("tech_itname")
                    .buildString("tech_ptbody")
                    .buildString("tech_level")
                    .buildString("tech_pic")
                    .buildString("b_id")
                    .buildString("introduction")
                    .buildString("ext_a")
                    .buildString("ext_b")
                    .buildString("ext_c");
            flag = esContext.createIndex(CommonConstants.INDEX_PREFIX_STRUCT, standard.getMapping());
            log.info("创建结构化索引成功");
        }catch (Exception e){
            flag = false;
            log.info("创建结构化索引失败:{}", e.getMessage());
        }
        return null;
    }

    @Override
    public AjaxResult searchIndex() {
        String result = HttpUtils.sendGet("http://"+eshost+":"+seport+"/"+CommonConstants.INDEX_PREFIX+"/_count");
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult deleteIndex() {
        esContext.deleteIndex(CommonConstants.INDEX_PREFIX);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult deleteStructIndex() {
        esContext.deleteIndex(CommonConstants.INDEX_PREFIX_STRUCT);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult addContent(ByDb15 byDb15) {
        List<ByDb15> list = iByDb15Service.selectStandardList(byDb15);
        log.info("同步开始----{}", DateTime.now());
        for(ByDb15 byDb151 : list){
            Map<String, Object> doc = new HashMap<>();
            doc.put("id", byDb151.getId());
            doc.put("a100", byDb151.getA100());
            doc.put("a101", byDb151.getA101());
            doc.put("a102", byDb151.getA102());
            doc.put("a200", byDb151.getA200());
            doc.put("a204", byDb151.getA204());
            doc.put("a205", byDb151.getA205());
            doc.put("a206", byDb151.getA206());
            doc.put("a209", byDb151.getA209());
            doc.put("a298", byDb151.getA298());
            doc.put("a301", byDb151.getA301());
            doc.put("a302", byDb151.getA302());
            doc.put("a330", byDb151.getA330());
            doc.put("a409", byDb151.getA409());
            doc.put("a461", byDb151.getA461());
            doc.put("a462", byDb151.getA462());
            doc.put("a807", byDb151.getA807());
            doc.put("a825", byDb151.getA825());
            doc.put("a826", byDb151.getA826());
            doc.put("a835", byDb151.getA835());
            doc.put("a850", byDb151.getA850());
            doc.put("a893", byDb151.getA893());
            doc.put("a894", byDb151.getA826());
            doc.put("a895", byDb151.getA826());
            doc.put("filePath", byDb151.getA826());
            esContext.addDocument(CommonConstants.INDEX_PREFIX, String.valueOf(byDb151.getId()), doc);
        }
        log.info("同步结束----{}", DateTime.now());
        return null;
    }

    @Override
    public AjaxResult addContentByTableName(String tableName) {
        List<ByDb15> list =  iByDb15Service.selectByCommonList(tableName);
        log.info("同步开始----{}", DateTime.now());
        log.info("同步开始----{}", list.size());
        SimpleDateFormat parser = new SimpleDateFormat("yyyy-M-d");
        SimpleDateFormat formatterq = new SimpleDateFormat("yyyy-MM-dd");


        for(ByDb15 byDb151 : list){
            Map<String, Object> doc = new HashMap<>();
            doc.put("id", byDb151.getId());
            doc.put("a000", byDb151.getA000());
            doc.put("a001", byDb151.getA001());
            doc.put("a003", byDb151.getA003());
            doc.put("a010", byDb151.getA010());
            doc.put("a011", byDb151.getA011());
            doc.put("a100", byDb151.getA100());

            String a101 = byDb151.getA101();
            try{
                Date parsedDate = parser.parse(a101);
                String formattedDate = formatterq.format(parsedDate);
                doc.put("a101", formattedDate);
            }catch (Exception e){

            }

            doc.put("a102", byDb151.getA102());
            doc.put("a103", byDb151.getA103());
            doc.put("a104", byDb151.getA104());
            doc.put("a200", byDb151.getA200());
            doc.put("a203", byDb151.getA203());
            doc.put("a204", byDb151.getA204());
            doc.put("a205", byDb151.getA205());
            doc.put("a206", byDb151.getA206());
            doc.put("a207", byDb151.getA207());
            doc.put("a208", byDb151.getA208());
            doc.put("a209", byDb151.getA209());
            doc.put("a210", byDb151.getA210());
            doc.put("a298", byDb151.getA298());
            doc.put("a300", byDb151.getA300());
            doc.put("a301", byDb151.getA301());
            doc.put("a302", byDb151.getA302());
            doc.put("a304", byDb151.getA304());
            doc.put("a305", byDb151.getA305());
            doc.put("a309", byDb151.getA309());
            doc.put("a310", byDb151.getA310());
            doc.put("a311", byDb151.getA311());
            doc.put("a330", byDb151.getA330());
            doc.put("a331", byDb151.getA331());
            doc.put("a390", byDb151.getA390());
            doc.put("a409", byDb151.getA409());
            doc.put("a448", byDb151.getA448());
            doc.put("a461", byDb151.getA461());
            doc.put("a462", byDb151.getA462());
            doc.put("a502", byDb151.getA502());
            doc.put("a505", byDb151.getA505());
            doc.put("a800", byDb151.getA800());
            doc.put("a805", byDb151.getA805());
            doc.put("a807", byDb151.getA807());
            doc.put("a808", byDb151.getA808());
            doc.put("a820", byDb151.getA820());
            doc.put("a821", byDb151.getA821());
            doc.put("a823", byDb151.getA823());
            doc.put("a824", byDb151.getA824());
            doc.put("a825", byDb151.getA825());
            doc.put("a826", byDb151.getA826());
            doc.put("a835", byDb151.getA835());
            doc.put("a840", byDb151.getA840());
            doc.put("a837", byDb151.getA837());
            doc.put("a845", byDb151.getA845());
            doc.put("a846", byDb151.getA846());
            doc.put("a847", byDb151.getA847());
            doc.put("a850", byDb151.getA850());
            doc.put("a860", byDb151.getA860());
            doc.put("a861", byDb151.getA861());
            doc.put("a863", byDb151.getA863());
            doc.put("a864", byDb151.getA864());
            doc.put("a865", byDb151.getA865());
            doc.put("a866", byDb151.getA866());
            doc.put("a870", byDb151.getA870());
            doc.put("a871", byDb151.getA871());
            doc.put("a885", byDb151.getA885());
            doc.put("a886", byDb151.getA886());
            doc.put("a890", byDb151.getA890());
            doc.put("a891", byDb151.getA891());
            doc.put("a892", byDb151.getA892());
            doc.put("a893", byDb151.getA893());
            doc.put("a894", byDb151.getA894());
            doc.put("a895", byDb151.getA895());
            doc.put("create_time", byDb151.getCreateTime());
            doc.put("textAddress", byDb151.getTextAddress());
            doc.put("filePath", byDb151.getFilePath());
            doc.put("reserve_a", byDb151.getReserveA());
            doc.put("reserve_b", byDb151.getReserveB());
            doc.put("reserve_c", byDb151.getReserveC());
            doc.put("reserve_d", byDb151.getReserveD());
            doc.put("reserve_e", byDb151.getReserveE());
            doc.put("table_name", byDb151.getTableName());
            esContext.addDocument(CommonConstants.INDEX_PREFIX, String.valueOf(byDb151.getId()), doc);
        }
        log.info("同步结束----{}", DateTime.now());
        return AjaxResult.success();
    }

    @Override
    public AjaxResult addContentByTableNameCol(String tableName) {
        List<ByDb15> list =  iByDb15Service.selectByCommonList(tableName);
        log.info("同步开始----{}", DateTime.now());
        log.info("同步开始----{}", list.size());
        SimpleDateFormat parser = new SimpleDateFormat("yyyy-M-d");
        SimpleDateFormat formatterq = new SimpleDateFormat("yyyy-MM-dd");


        for(ByDb15 byDb151 : list){
            //判重
            Map<String,Object> mapNew  = esContext.getDocument(CommonConstants.INDEX_PREFIX,byDb151.getId(),Map.class);
            if(mapNew != null){


            }else{
                Map<String, Object> doc = new HashMap<>();
                doc.put("id", byDb151.getId());
                doc.put("a000", byDb151.getA000());
                doc.put("a001", byDb151.getA001());
                doc.put("a003", byDb151.getA003());
                doc.put("a010", byDb151.getA010());
                doc.put("a011", byDb151.getA011());
                doc.put("a100", byDb151.getA100());

                String a101 = byDb151.getA101();
                try{
                    Date parsedDate = parser.parse(a101);
                    String formattedDate = formatterq.format(parsedDate);
                    doc.put("a101", formattedDate);
                }catch (Exception e){

                }

                doc.put("a102", byDb151.getA102());
                doc.put("a103", byDb151.getA103());
                doc.put("a104", byDb151.getA104());
                doc.put("a200", byDb151.getA200());
                doc.put("a203", byDb151.getA203());
                doc.put("a204", byDb151.getA204());
                doc.put("a205", byDb151.getA205());
                doc.put("a206", byDb151.getA206());
                doc.put("a207", byDb151.getA207());
                doc.put("a208", byDb151.getA208());
                doc.put("a209", byDb151.getA209());
                doc.put("a210", byDb151.getA210());
                doc.put("a298", byDb151.getA298());
                doc.put("a300", byDb151.getA300());
                doc.put("a301", byDb151.getA301());
                doc.put("a302", byDb151.getA302());
                doc.put("a304", byDb151.getA304());
                doc.put("a305", byDb151.getA305());
                doc.put("a309", byDb151.getA309());
                doc.put("a310", byDb151.getA310());
                doc.put("a311", byDb151.getA311());
                doc.put("a330", byDb151.getA330());
                doc.put("a331", byDb151.getA331());
                doc.put("a390", byDb151.getA390());
                doc.put("a409", byDb151.getA409());
                doc.put("a448", byDb151.getA448());
                doc.put("a461", byDb151.getA461());
                doc.put("a462", byDb151.getA462());
                doc.put("a502", byDb151.getA502());
                doc.put("a505", byDb151.getA505());
                doc.put("a800", byDb151.getA800());
                doc.put("a805", byDb151.getA805());
                doc.put("a807", byDb151.getA807());
                doc.put("a808", byDb151.getA808());
                doc.put("a820", byDb151.getA820());
                doc.put("a821", byDb151.getA821());
                doc.put("a823", byDb151.getA823());
                doc.put("a824", byDb151.getA824());
                doc.put("a825", byDb151.getA825());
                doc.put("a826", byDb151.getA826());
                doc.put("a835", byDb151.getA835());
                doc.put("a840", byDb151.getA840());
                doc.put("a837", byDb151.getA837());
                doc.put("a845", byDb151.getA845());
                doc.put("a846", byDb151.getA846());
                doc.put("a847", byDb151.getA847());
                doc.put("a850", byDb151.getA850());
                doc.put("a860", byDb151.getA860());
                doc.put("a861", byDb151.getA861());
                doc.put("a863", byDb151.getA863());
                doc.put("a864", byDb151.getA864());
                doc.put("a865", byDb151.getA865());
                doc.put("a866", byDb151.getA866());
                doc.put("a870", byDb151.getA870());
                doc.put("a871", byDb151.getA871());
                doc.put("a885", byDb151.getA885());
                doc.put("a886", byDb151.getA886());
                doc.put("a890", byDb151.getA890());
                doc.put("a891", byDb151.getA891());
                doc.put("a892", byDb151.getA892());
                doc.put("a893", byDb151.getA893());
                doc.put("a894", byDb151.getA894());
                doc.put("a895", byDb151.getA895());
                doc.put("create_time", byDb151.getCreateTime());
                doc.put("textAddress", byDb151.getTextAddress());
                doc.put("filePath", byDb151.getFilePath());
                doc.put("reserve_a", byDb151.getReserveA());
                doc.put("reserve_b", byDb151.getReserveB());
                doc.put("reserve_c", byDb151.getReserveC());
                doc.put("reserve_d", byDb151.getReserveD());
                doc.put("reserve_e", byDb151.getReserveE());
                doc.put("table_name", byDb151.getTableName());
                esContext.addDocument(CommonConstants.INDEX_PREFIX, String.valueOf(byDb151.getId()), doc);

            }

        }
        log.info("同步结束----{}", DateTime.now());
        return AjaxResult.success();
    }

    @Override
    public AjaxResult addContentByTableNameAll() {
        List<String> tableNameList = CommonConstants.tableList;
        try {
            for(String tName : tableNameList){
                String taName = "standard_"+tName;
                log.info("完全同步开始----{}", DateTime.now());
                log.info("完全同步表明----{}", taName);
                List<ByDb15> list =  iByDb15Service.selectByCommonList(taName);
                log.info("完全同步数据量----{}", list.size());
                for(ByDb15 byDb151 : list) {
                    Map<String, Object> doc = new HashMap<>();
                    doc.put("id", byDb151.getId());
                    doc.put("a100", byDb151.getA100());
                    doc.put("a101", byDb151.getA101());
                    doc.put("a102", byDb151.getA102());
                    doc.put("a200", byDb151.getA200());
                    doc.put("a204", byDb151.getA204());
                    doc.put("a205", byDb151.getA205());
                    doc.put("a206", byDb151.getA206());
                    doc.put("a209", byDb151.getA209());
                    doc.put("a298", byDb151.getA298());
                    doc.put("a301", byDb151.getA301());
                    doc.put("a302", byDb151.getA302());
                    doc.put("a310", byDb151.getA310());
                    doc.put("a330", byDb151.getA330());
                    doc.put("a409", byDb151.getA409());
                    doc.put("a461", byDb151.getA461());
                    doc.put("a462", byDb151.getA462());
                    doc.put("a807", byDb151.getA807());
                    doc.put("a825", byDb151.getA825());
                    doc.put("a826", byDb151.getA826());
                    doc.put("a835", byDb151.getA835());
                    doc.put("a850", byDb151.getA850());
                    doc.put("a893", byDb151.getA893());
                    doc.put("a894", byDb151.getA894());
                    doc.put("a895", byDb151.getA895());
                    doc.put("filePath", byDb151.getA826());
                    doc.put("table_name", taName);
                    esContext.addDocument(CommonConstants.INDEX_PREFIX, String.valueOf(byDb151.getId()), doc);
                }
            }
        }catch (Exception e){
            log.error("同步异常----{}", e.getMessage());
        }
        return null;
    }

    @Override
    public AjaxResult synStruct() {
        try {
            List<StandardDb15W> list =  iStandardDb15WService.selectStandardDb15WList(null);
            log.info("结构化完全同步数据量----{}", list.size());
            for(StandardDb15W standardDb15W : list) {
                Map<String, Object> doc = new HashMap<>();
                doc.put("id", standardDb15W.getId());
                doc.put("type", standardDb15W.getType());
                doc.put("clg_id", standardDb15W.getClgId());
                doc.put("clg_name", standardDb15W.getClgName());
                doc.put("n_docum", standardDb15W.getnDocum());
                doc.put("app_id", standardDb15W.getAppId());
                doc.put("app_body", standardDb15W.getAppBody());
                doc.put("ref_item", standardDb15W.getRefItem());
                doc.put("t_id", standardDb15W.gettId());
                doc.put("t_cn", standardDb15W.gettCn());
                doc.put("t_en", standardDb15W.gettEn());
                doc.put("t_def", standardDb15W.gettDef());
                doc.put("t_note", standardDb15W.gettNote());
                doc.put("t_exp", standardDb15W.gettExp());
                doc.put("pic", standardDb15W.getPic());
                doc.put("tech_itid", standardDb15W.getTechItid());
                doc.put("tech_itname", standardDb15W.getTechItname());
                doc.put("tech_ptbody", standardDb15W.getTechPtbody());
                doc.put("tech_level", standardDb15W.getTechLevel());
                doc.put("tech_pic", standardDb15W.getTechPic());
                doc.put("b_id", standardDb15W.getbId());
                doc.put("introduction", standardDb15W.getIntroduction());
                doc.put("ext_a", standardDb15W.getExtA());
                doc.put("ext_b", standardDb15W.getExtB());
                doc.put("ext_c", standardDb15W.getExtC());
                esContext.addDocument(CommonConstants.INDEX_PREFIX_STRUCT, String.valueOf(standardDb15W.getId()), doc);
            }
        }catch (Exception e){
            log.error("同步异常----{}", e.getMessage());
        }
        return null;
    }


    @Override
    public AjaxResult udpatePubTimeAll() {
        List<String> tableNameList = CommonConstants.tablePriceList;
        try {
            for(String tName : tableNameList){
                String taName = "standard_"+tName;
                log.info("修改时间数据开始----{}", DateTime.now());
                log.info("修改时间数据开始----{}", taName);
                List<ByDb15> list =  iByDb15Service.selectByCommonList(taName);
                log.info("完全同步数据量----{}", list.size());
                for(ByDb15 byDb151 : list) {
                    //es先获取
                    ByDb15 byDb15 = esContext.getDocument(CommonConstants.INDEX_PREFIX,byDb151.getId(),ByDb15.class);
                    //判断是否符合
                    if(StringUtils.isNotEmpty(byDb15.getA101())){
                    }
                    //es修改
                    //更新数据库
                    Map<String, Object> doc = new HashMap<>();
                    doc.put("id", byDb151.getId());
                    doc.put("a100", byDb151.getA100());
                    doc.put("a101", byDb151.getA101());
                    doc.put("filePath", byDb151.getA826());
                    doc.put("table_name", taName);
                    esContext.addDocument(CommonConstants.INDEX_PREFIX, String.valueOf(byDb151.getId()), doc);
                }
            }
        }catch (Exception e){
            log.error("同步异常----{}", e.getMessage());
        }
        return null;
    }

    @Override
    public AjaxResult insertNewIndex() {
        List<String> tableNameList = CommonConstants.tableListPartGB;

            for(String tName : tableNameList){
                String taName = "standard_"+tName;
                log.info("修改时间数据开始----{}", DateTime.now());
                log.info("修改时间数据开始----{}", taName);
                List<ByDb15> list =  iByDb15Service.selectByCommonList(taName);
                log.info("完全同步数据量----{}", list.size());
                for(ByDb15 byDb151 : list) {
                    //判断新索引是否存在
                    Map<String,Object> mapNew = esContext.getDocument(CommonConstants.NEW_INDEX_PREFIX,byDb151.getId(),Map.class);
                    if(null != mapNew){
                        String a101 = byDb151.getA101();
                        if(StringUtils.isNotEmpty(a101)){
                            SimpleDateFormat parser = new SimpleDateFormat("yyyy-M-d");
                            SimpleDateFormat formatterq = new SimpleDateFormat("yyyy-MM-dd");
                            try {
                                Date parsedDate = parser.parse(a101);
                                String formattedDate = formatterq.format(parsedDate);
                                mapNew.put("a101",formattedDate);
                                mapNew.put("a850","GB");
                                esContext.modifyDocumentById(CommonConstants.NEW_INDEX_PREFIX, String.valueOf(byDb151.getId()), mapNew);
                            } catch (DateTimeParseException | ParseException e) {

                                continue;
                            }
                        }


                    }else{
                        //es先获取
                        Map<String,Object> map = esContext.getDocument(CommonConstants.INDEX_PREFIX,byDb151.getId(),Map.class);
                        if(map!=null && map.containsKey("a101")){
                            //判断是否符合
//                            log.info(map.get("id").toString());
                            if(StringUtils.isNotEmpty(map.get("a101").toString())){
                                //如果符合插入新索引
                                String a101 = map.get("a101").toString();

                                SimpleDateFormat parser = new SimpleDateFormat("yyyy-M-d");
                                SimpleDateFormat formatterq = new SimpleDateFormat("yyyy-MM-dd");
                                try {
                                    Date parsedDate = parser.parse(a101);
                                    String formattedDate = formatterq.format(parsedDate);
                                    map.put("a101",formattedDate);
                                    esContext.addDocument(CommonConstants.NEW_INDEX_PREFIX, String.valueOf(byDb151.getId()), map);
                                } catch (DateTimeParseException | ParseException e) {

                                    continue;
                                }

                            }else{
                                //添加

                                Map<String, Object> doc = new HashMap<>();
                                doc.put("id", byDb151.getId());
                                doc.put("a100", byDb151.getA100());
//                            doc.put("a101", null);
                                doc.put("a102", byDb151.getA102());
                                doc.put("a200", byDb151.getA200());
                                doc.put("a204", byDb151.getA204());
                                doc.put("a205", byDb151.getA205());
                                doc.put("a206", byDb151.getA206());
                                doc.put("a209", byDb151.getA209());
                                doc.put("a298", byDb151.getA298());
                                doc.put("a301", byDb151.getA301());
                                doc.put("a302", byDb151.getA302());
                                doc.put("a310", byDb151.getA310());
                                doc.put("a330", byDb151.getA330());
                                doc.put("a409", byDb151.getA409());
                                doc.put("a461", byDb151.getA461());
                                doc.put("a462", byDb151.getA462());
                                doc.put("a807", byDb151.getA807());
                                doc.put("a825", byDb151.getA825());
                                doc.put("a826", byDb151.getA826());
                                doc.put("a835", byDb151.getA835());
                                doc.put("a850", byDb151.getA850());
                                doc.put("a893", byDb151.getA893());
                                doc.put("a894", byDb151.getA894());
                                doc.put("a895", byDb151.getA895());
                                doc.put("filePath", byDb151.getA826());
                                doc.put("table_name", taName);
                                esContext.addDocument(CommonConstants.NEW_INDEX_PREFIX, String.valueOf(byDb151.getId()), doc);
                            }
                        }else{
                            //添加
                            Map<String, Object> doc = new HashMap<>();
                            doc.put("id", byDb151.getId());
                            doc.put("a100", byDb151.getA100());
//                        doc.put("a101", byDb151.getA101());
                            doc.put("a102", byDb151.getA102());
                            doc.put("a200", byDb151.getA200());
                            doc.put("a204", byDb151.getA204());
                            doc.put("a205", byDb151.getA205());
                            doc.put("a206", byDb151.getA206());
                            doc.put("a209", byDb151.getA209());
                            doc.put("a298", byDb151.getA298());
                            doc.put("a301", byDb151.getA301());
                            doc.put("a302", byDb151.getA302());
                            doc.put("a310", byDb151.getA310());
                            doc.put("a330", byDb151.getA330());
                            doc.put("a409", byDb151.getA409());
                            doc.put("a461", byDb151.getA461());
                            doc.put("a462", byDb151.getA462());
                            doc.put("a807", byDb151.getA807());
                            doc.put("a825", byDb151.getA825());
                            doc.put("a826", byDb151.getA826());
                            doc.put("a835", byDb151.getA835());
                            doc.put("a850", byDb151.getA850());
                            doc.put("a893", byDb151.getA893());
                            doc.put("a894", byDb151.getA894());
                            doc.put("a895", byDb151.getA895());
                            doc.put("filePath", byDb151.getA826());
                            doc.put("table_name", taName);
                            esContext.addDocument(CommonConstants.NEW_INDEX_PREFIX, String.valueOf(byDb151.getId()), doc);
                        }
                    }
                }
            }

        return null;
    }

    @Override
    public AjaxResult addContentByTableNameAllPrice() {
        List<String> tableNameList = CommonConstants.tablePriceList;
        try {
            for(String tName : tableNameList){
                String taName = "standard_"+tName;
                log.info("完全同步价格开始----{}", DateTime.now());
                log.info("完全同步价格表名----{}", taName);
                List<ByDb15> list =  iByDb15Service.selectByCommonList(taName);
                log.info("完全同步价格数据量----{}", list.size());
                for(ByDb15 byDb151 : list) {
                    Map<String, Object> doc = new HashMap<>();
                    doc.put("a310", byDb151.getA310());
                    esContext.modifyDocumentById(CommonConstants.INDEX_PREFIX, String.valueOf(byDb151.getId()), doc);
                }
            }
        }catch (Exception e){
            log.error("同步异常----{}", e.getMessage());
        }
        return null;
    }

    @Override
    public AjaxResult deleteIndexDocAll(String indexName) {
        esContext.deleteDocumentAll(CommonConstants.INDEX_PREFIX);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult deleteIndexDocById(String id) {
        esContext.deleteDocumentById(CommonConstants.INDEX_PREFIX, id);
        return AjaxResult.success();
    }

    @Override
    public TableDataInfo search(String keyNum) {

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(200);
        rspData.setMsg("查询成功");
        rspData.setRows(Collections.emptyList());
        try {
            List<Map<String, Object>> dataList = new ArrayList<>();
            ESBuilder b = ESBuilder.builder().build();
            if (StringUtils.isNotEmpty(keyNum)) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchField, keyNum);
                b.buildBoolMust(build.getQuery());
            }
//            if (StringUtils.isNotEmpty(syscode)) {
//                b.buildBoolMustWildCard("class_system", syscode);
//            }
//            Integer from = (pageNum - 1) * pageSize;
//            List<String> indexList = getIndexByDatabaseCode(code, replace);
//            if (indexList.isEmpty()) {
//                return rspData;
//            }
            List<String> indexList = new ArrayList<>();
            indexList.add("new_standard_all");
            ESBuilder highLight = ESBuilder.builder().build().buildHighLight("<span style=\"color:red\">", "</span>", 5, 50, Arrays.asList("a100"));
            ESBuilder sort = ESBuilder.builder().build().buildSort("a101", "desc", false);
            SearchResponse<Object> response = esContext.searchIndex(indexList, b, 1, 10, sort.getSorts(), highLight.getHighlight());
            // 数据量太大 log.info("查询到结果:{}", response ==null?"":JSON.toJSONString(response.hits().hits().stream().map(Hit::source).collect(Collectors.toList())));
            if (response != null) {
                for (Hit<Object> hit : response.hits().hits()) {
                    Map<String, Object> map = new HashMap<>();
                    Map<String, Object> source = (Map<String, Object>) hit.source();
                    Map<String, List<String>> highlight = hit.highlight();
                    List<String> a100List = highlight.get("a100");
                    map.put("a100", highlight.get("a100"));

//                    List<String> abstractList = highlight.get("abstract");
//                    List<String> keywordList = highlight.get("keyword");
//                    map.put("title", source.get("title"));
//                    map.put("abstract", source.get("abstract"));
//                    map.put("keyword", source.get("keyword"));
//                    if (StringUtils.isNotEmpty(syscode)) {
                        if (a100List != null) {
                            map.put("a100", a100List.get(0));
                        }
//                        if (abstractList != null) {
//                            map.put("abstract", String.join(",", abstractList));
//                        }
//                        if (keywordList != null) {
//                            map.put("keyword", String.join(",", keywordList));
//                        }
//                    }
//                    map.put("createTime", source.get("create_time"));
                    String index = hit.index();
                    map.put("index", index);
                    map.put("id", hit.id());
                    map.put("author", source.get("author"));

                    map.put("a101", source.get("a101"));
                    map.put("a102", source.get("a102"));
                    map.put("a200", source.get("a200"));
                    map.put("a205", source.get("a205"));
                    map.put("a206", source.get("a206"));
                    map.put("a209", source.get("a209"));
                    map.put("a298", source.get("a298"));
                    map.put("a302", source.get("a302"));
                    map.put("a330", source.get("a330"));
                    map.put("a331", source.get("a331"));
                    map.put("a461", source.get("a461"));
                    map.put("a310", source.get("a310"));
                    map.put("a462", source.get("a462"));
                    map.put("a825", source.get("a825"));
                    map.put("a826", source.get("a826"));

//                    List<Map<String, String>> fileList = dataContext.excuteQueryList("select filelinkurl,filesize from " + index.replace(CommonConstants.INDEX_PREFIX, "") + "_metadata_file where recordid = " + hit.id());
//                    if (!fileList.isEmpty()) {
//                        map.put("fileUrl", fileList.get(0).get("filelinkurl"));
//                        map.put("fileSize", fileList.get(0).get("filesize") == null ? 0 : new BigDecimal(fileList.get(0).get("filesize")).divide(new BigDecimal(1024), 0, RoundingMode.HALF_UP));
//                        map.put("fileSizeStr", fileList.get(0).get("filesize") == null ? "0B" : FileUtil.formatFileSize(Long.parseLong(fileList.get(0).get("filesize"))));
//                        if (fileList.get(0).get("filelinkurl").lastIndexOf(".") > 0) {
//                            String docType = fileList.get(0).get("filelinkurl").substring(fileList.get(0).get("filelinkurl").lastIndexOf(".") + 1);
//                            String[] allow = allowDocType.split(",");
//                            map.put("canPreview", Arrays.asList(allow).contains(docType));
//                        }
//                    }
                    dataList.add(map);
                }
            }
            rspData.setRows(dataList);
            rspData.setTotal(response == null ? 0 : response.hits().total().value());
            return rspData;
        } catch (Exception e) {
            e.printStackTrace();
            return rspData;
        } finally {
//            try {
//                userService.saveUserBehaviorOfSearch(keyword);
//            } catch (Exception e) {
//            }
        }
    }

    @Override
    public List<ByDb15> searchAll(String keyNum,String a101,String a200,String standardType) {
        List<ByDb15> list = new ArrayList<>();
        try {
            ESBuilder b = ESBuilder.builder().build();
            List<String> indexList = new ArrayList<>();
            indexList.add("new_standard_all");
//            ESBuilder build = ESBuilder.builder().build();
//            build.buildQueryMutiMath(CommonConstants.searchFieldExport, keyNum);
            log.info("导出关键词{}",keyNum);

            if(StringUtils.isNotEmpty(keyNum) ){
                ESBuilder build = ESBuilder.builder().build();
                build.buildBoolShould("a298", keyNum);
//            build.buildQueryMutiMath(CommonConstants.searchFieldA302, keyNum);
//            build.buildQueryMutiMath(CommonConstants.searchFieldA895, keyNum);
//            build.buildQueryMutiMath(CommonConstants.searchFieldA209, keyNum);
                build.buildBoolShould("a302", keyNum);
                build.buildBoolShould("a895", keyNum);
                build.buildBoolShould("a209", keyNum);
                build.buildBoolShould("a807", keyNum);
                build.buildBoolShould("a808", keyNum);
                build.buildBoolShould("a330", keyNum);
                build.buildBoolShould("a331", keyNum);
                b.buildBoolMust(build.getBool());
            }
            if(StringUtils.isNotEmpty(a200)){
                b.buildBoolMust("a200", a200);
            }
            if (StringUtils.isNotEmpty(a101)) {
//                ESBuilder build = ESBuilder.builder().build();
                // 构建范围查询（添加到 filterQuerys 列表）
                String year = a101;
                String startDate = year + "-01-01";
                String endDate = year + "-12-31";
                b.buildQueryListRange(CommonConstants.searchFieldA101, "gt", startDate);
                b.buildQueryListRange(CommonConstants.searchFieldA101, "lt", endDate);
                // 从 filterQuerys 中获取查询条件（注意：filterQuerys 可能为 null 或空
            }
            if(StringUtils.isNotEmpty(standardType)){
                if(standardType.equals("国家标准")){
                    b.buildBoolMust("a850",CommonConstants.gbString);
                }
                if(standardType.equals("地方标准")){
                    b.buildBoolMust("a850",CommonConstants.dbString);
                }
                if(standardType.equals("行业标准")){
                    b.buildBoolMust("a850",CommonConstants.hbString);
                }
            }

            SearchResponse<Object> response = esContext.searchIndex(indexList, b, 0, 10000,null,null);
            // 数据量太大 log.info("查询到结果:{}", response ==null?"":JSON.toJSONString(response.hits().hits().stream().map(Hit::source).collect(Collectors.toList())));
            if (response != null) {
                log.info("数据总量----{}",response.hits().hits().size());
                for (Hit<Object> hit : response.hits().hits()) {
                    Map<String, Object> source = (Map<String, Object>) hit.source();
                    ByDb15 byDb15 = mapToEntity(source, ByDb15.class);
                    list.add(byDb15);
                }
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return list;
        } finally {
        }
    }

    @Override
    public TableDataInfo searchAll(ByDb15 byDb15, Integer pageNum, Integer pageSize) {
        String keyNum = byDb15.getA298();
        String standardType = byDb15.getA850();
        String a200 = byDb15.getA200();
        String a101 = byDb15.getA101();


        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(200);
        rspData.setMsg("查询成功");
        rspData.setRows(Collections.emptyList());


        try {
            List<Map<String, Object>> dataList = new ArrayList<>();
            ESBuilder b = ESBuilder.builder().build();
            List<String> indexList = new ArrayList<>();
            indexList.add("new_standard_all");


            if(StringUtils.isNotEmpty(keyNum) ){
                ESBuilder build = ESBuilder.builder().build();
                build.buildBoolShould("a298", keyNum);
                build.buildBoolShould("a302", keyNum);
                build.buildBoolShould("a895", keyNum);
                build.buildBoolShould("a209", keyNum);
                build.buildBoolShould("a807", keyNum);
                build.buildBoolShould("a808", keyNum);
                build.buildBoolShould("a330", keyNum);
                build.buildBoolShould("a331", keyNum);
                b.buildBoolMust(build.getBool());
            }
            if(StringUtils.isNotEmpty(a200)){
                b.buildBoolMust("a200", a200);
            }
            if (StringUtils.isNotEmpty(a101)) {
//                ESBuilder build = ESBuilder.builder().build();
                // 构建范围查询（添加到 filterQuerys 列表）
                String year = a101;
                String startDate = year + "-01-01";
                String endDate = year + "-12-31";
                b.buildQueryListRange(CommonConstants.searchFieldA101, "gt", startDate);
                b.buildQueryListRange(CommonConstants.searchFieldA101, "lt", endDate);
                // 从 filterQuerys 中获取查询条件（注意：filterQuerys 可能为 null 或空
            }
            if(StringUtils.isNotEmpty(standardType)){
                if(standardType.equals("国家标准")){
                    b.buildBoolMust("a850",CommonConstants.gbString);
                }
                if(standardType.equals("地方标准")){
                    b.buildBoolMust("a850",CommonConstants.dbString);
                }
                if(standardType.equals("行业标准")){
                    b.buildBoolMust("a850",CommonConstants.hbString);
                }
            }

            Integer from = (pageNum - 1) * pageSize;

            SearchResponse<Object> response = esContext.searchIndex(indexList, b, from, pageSize,null,null);
            // 数据量太大 log.info("查询到结果:{}", response ==null?"":JSON.toJSONString(response.hits().hits().stream().map(Hit::source).collect(Collectors.toList())));
            if (response != null) {
                for (Hit<Object> hit : response.hits().hits()) {
                    Map<String, Object> map = new HashMap<>();
                    Map<String, Object> source = (Map<String, Object>) hit.source();
                    String index = hit.index();
                    map.put("a100", source.get("a100"));
                    map.put("index", index);
                    map.put("id", hit.id());
                    map.put("author", source.get("author"));
                    map.put("a101", source.get("a101"));
                    map.put("a102", source.get("a102"));
                    map.put("a200", source.get("a200"));
                    map.put("a205", source.get("a205"));
                    map.put("a206", source.get("a206"));
                    map.put("a209", source.get("a209"));
                    map.put("a298", source.get("a298"));
                    map.put("a302", source.get("a302"));
                    map.put("a330", source.get("a330"));
                    map.put("a331", source.get("a331"));
                    map.put("a461", source.get("a461"));
                    map.put("a310", source.get("a310"));
                    map.put("a462", source.get("a462"));
                    map.put("a825", source.get("a825"));
                    map.put("a826", source.get("a826"));
                    dataList.add(map);
                }
            }
            rspData.setRows(dataList);
            rspData.setTotal(response == null ? 0 : response.hits().total().value());
            return rspData;
        } catch (Exception e) {
            e.printStackTrace();
            return rspData;
        } finally {
        }
    }


    public TableDataInfo searchAllBak(ByDb15 byDb15, Integer pageNum, Integer pageSize) {
        String keyNum = byDb15.getA298();
//        keyNum = addSlashTIfGbWithNumber(keyNum);
//        String standardType = byDb15.getA850();
//        String a200 = byDb15.getA200();
//        String a101 = byDb15.getA101();


        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(200);
        rspData.setMsg("查询成功");
        rspData.setRows(Collections.emptyList());


        try {
            List<Map<String, Object>> dataList = new ArrayList<>();
            ESBuilder b = ESBuilder.builder().build();
            List<String> indexList = new ArrayList<>();
            indexList.add("new_standard_all");
            List<String> highlightList = new ArrayList<>();
            highlightList.add("a100");
            highlightList.add("a298");
            if(StringUtils.isNotEmpty(keyNum) ){
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMathDb15(CommonConstants.searchFieldA101List, keyNum);
                build.buildQueryMutiMath(CommonConstants.searchFieldA100A298, keyNum);
                b.buildBoolMust(build.getQuery());

            }
            if (StringUtils.isNotNull(byDb15.getCategory()) && StringUtils.isNotEmpty(byDb15.getCategory())) {
                String category = byDb15.getCategory();
                if(category.equals("all")){
                    ESBuilder build = ESBuilder.builder().build();
                    build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.totalString);
                    b.buildBoolMust(build.getQuery());
                }else if(category.equals("gb")){
                    ESBuilder build = ESBuilder.builder().build();
                    build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.gbString);
                    b.buildBoolMust(build.getQuery());
                }else if(category.equals("hb")){
                    ESBuilder build = ESBuilder.builder().build();
                    build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.hbString);
                    b.buildBoolMust(build.getQuery());
                }else if(category.equals("db")){
                    ESBuilder build = ESBuilder.builder().build();
                    build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.dbString);
                    b.buildBoolMust(build.getQuery());
                }else if(category.equals("tb")){
                    ESBuilder build = ESBuilder.builder().build();
                    build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.tbString);
                    b.buildBoolMust(build.getQuery());
                }else if(category.equals("gw")){
                    ESBuilder build = ESBuilder.builder().build();
                    build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.gwString);
                    b.buildBoolMust(build.getQuery());
                }
            }
            ESBuilder highLight = ESBuilder.builder().build().buildHighLight("<span style=\"color:red\">", "</span>", 5, 50, highlightList);

            Integer from = (pageNum - 1) * pageSize;

            SearchResponse<Object> response = esContext.searchIndex(indexList, b, from, pageSize,null,highLight.getHighlight());
            // 数据量太大 log.info("查询到结果:{}", response ==null?"":JSON.toJSONString(response.hits().hits().stream().map(Hit::source).collect(Collectors.toList())));
            if (response != null) {
                for (Hit<Object> hit : response.hits().hits()) {
                    Map<String, Object> map = new HashMap<>();
                    Map<String, Object> source = (Map<String, Object>) hit.source();
                    Map<String, List<String>> highlight = hit.highlight();
                    List<String> a100List = highlight.get("a100");
                    List<String> a298List = highlight.get("a298");
                    List<String> a302List = highlight.get("a302");
                    List<String> a895List = highlight.get("a895");
                    List<String> a209List = highlight.get("a209");

                    map.put("a100", highlight.get("a100"));
                    map.put("a298", highlight.get("a298"));
                    map.put("a302", highlight.get("a302"));
                    map.put("a895", highlight.get("a895"));
                    map.put("a209", highlight.get("a209"));


                    if (a100List != null) {
                        map.put("a100", a100List.get(0));
                    }else{
                        map.put("a100", source.get("a100"));
                    }
                    if (a298List != null) {
                        map.put("a298", a298List.get(0));
                    }else{
                        map.put("a298", source.get("a298"));
                    }
                    if (a302List != null) {
                        map.put("a302", a302List.get(0));
                    }else{
                        map.put("a302", source.get("a302"));
                    }
                    if (a895List != null) {
                        map.put("a895", a895List.get(0));
                    }else{
                        map.put("a895", source.get("a895"));
                    }
                    if (a209List != null) {
                        map.put("a209", a209List.get(0));
                    }else{
                        map.put("a209", source.get("a209"));
                    }


                    String index = hit.index();
                    map.put("index", index);
                    map.put("id", hit.id());


                    map.put("a101", source.get("a101"));
//                    if(){
//
//                    }
                    map.put("a102", source.get("a102"));
                    map.put("a200", source.get("a200"));
                    map.put("a205", source.get("a205"));
                    map.put("a206", source.get("a206"));
                    map.put("a895", source.get("a895"));
                    map.put("a330", source.get("a330"));
                    map.put("a331", source.get("a331"));
                    map.put("a461", source.get("a461"));
                    map.put("a462", source.get("a462"));
                    map.put("a825", source.get("a825"));
                    map.put("a826", source.get("a826"));
                    map.put("a310", source.get("a310"));
                    map.put("filePath", source.get("filePath"));
                    map.put("table_name", source.get("table_name"));
                    dataList.add(map);
                }
            }
            String category = byDb15.getCategory();
            if(category.equals("all")){
                long gbtotal = searchGbAllB(byDb15);
                long hbtotal = searchHbAllB(byDb15);
                long dbtotal = searchDbAllB(byDb15);
                long tbtotal = searchTbAllB(byDb15);
                long gwtotal = searchGwAllB(byDb15);
                rspData.setGbTotal(gbtotal);
                rspData.setDbTotal(dbtotal);
                rspData.setHbTotal(hbtotal);
                rspData.setTbTotal(tbtotal);
                rspData.setGwTotal(gwtotal);
            }
            rspData.setRows(dataList);
            rspData.setTotal(response == null ? 0 : response.hits().total().value());
            return rspData;
        } catch (Exception e) {
            e.printStackTrace();
            return rspData;
        } finally {
        }
    }

    @Override
    public TableDataInfo searchAllB(ByDb15 byDb15, Integer pageNum, Integer pageSize) {
        String keyNum = byDb15.getA298();


        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(200);
        rspData.setMsg("查询成功");
        rspData.setRows(Collections.emptyList());
        try {

            List<Map<String, Object>> dataList = new ArrayList<>();
            List<String> highlightList = new ArrayList<>();
            highlightList.add("a100");
            highlightList.add("a298");

            highlightList.add("a850");
            ESBuilder b = ESBuilder.builder().build();

            if(StringUtils.isNotEmpty(keyNum)){
                ESBuilder build = ESBuilder.builder().build();
//                build.buildQueryMutiMathDb15(CommonConstants.searchFieldA100, keyNum);
//                build.buildQueryMutiMath(CommonConstants.searchFieldA100A298, keyNum);
                build.buildQueryMutiMathDb15All(CommonConstants.searchFieldA100A298, keyNum);
                b.buildBoolMust(build.getQuery());
            }

            if (StringUtils.isNotNull(byDb15.getA200()) && StringUtils.isNotEmpty(byDb15.getA200())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA200, byDb15.getA200());
                b.buildBoolMust(build.getQuery());
            }

            if (StringUtils.isNotNull(byDb15.getCategory()) && StringUtils.isNotEmpty(byDb15.getCategory())) {
                String category = byDb15.getCategory();
                if(category.equals("all")){
                    ESBuilder build = ESBuilder.builder().build();
                    build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.totalString);
                    b.buildBoolMust(build.getQuery());
                }else if(category.equals("gb")){
                    ESBuilder build = ESBuilder.builder().build();
                    build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.gbString);
                    b.buildBoolMust(build.getQuery());
                }else if(category.equals("hb")){
                    ESBuilder build = ESBuilder.builder().build();
                    build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.hbString);
                    b.buildBoolMust(build.getQuery());
                }else if(category.equals("db")){
                    ESBuilder build = ESBuilder.builder().build();
                    build.buildQueryMutiMath850bak(CommonConstants.searchFieldA850, CommonConstants.dbString);
                    b.buildBoolMust(build.getQuery());
                }else if(category.equals("tb")){
                    ESBuilder build = ESBuilder.builder().build();
                    build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.tbString);
                    b.buildBoolMust(build.getQuery());
                }else if(category.equals("gw")){
                    ESBuilder build = ESBuilder.builder().build();
                    build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.gwString);
                    b.buildBoolMust(build.getQuery());
                }
            }

            Integer from = (pageNum - 1) * pageSize;
            List<String> indexList = new ArrayList<>();
            indexList.add(CommonConstants.INDEX_PREFIX);
            ESBuilder highLight = ESBuilder.builder().build().buildHighLight("<span style=\"color:red\">", "</span>", 5, 50, highlightList);
            ESBuilder sort = ESBuilder.builder().build().buildSort("a101", "desc", false);
//            SearchResponse<Object> response = esContext.searchIndex(indexList, b, from, pageSize, sort.getSorts(), highLight.getHighlight());

            // 数据量太大 log.info("查询到结果:{}", response ==null?"":JSON.toJSONString(response.hits().hits().stream().map(Hit::source).collect(Collectors.toList())));

            long totalNum = 0L;
            String category = byDb15.getCategory();
            if(category.equals("all")){

                //拼凑list
                SearchResponse<Object> gbres = searchGbTotalK(byDb15,0,0,highlightList);
                SearchResponse<Object> hbres = searchHbTotalK(byDb15,0,0,highlightList);
                SearchResponse<Object> dbres = searchDbTotalK(byDb15,0,0,highlightList);
                SearchResponse<Object> tbres = searchTbTotalK(byDb15,0,0,highlightList);
                SearchResponse<Object> gwres = searchGwTotalK(byDb15,0,0,highlightList);

                long gbtotal = gbres == null ? 0 : gbres.hits().total().value();
                long hbtotal = hbres == null ? 0 : hbres.hits().total().value();
                long dbtotal = dbres == null ? 0 : dbres.hits().total().value();
                long tbtotal = tbres == null ? 0 : tbres.hits().total().value();
                long gwtotal = gwres == null ? 0 : gwres.hits().total().value();
                totalNum = gbtotal + hbtotal + dbtotal + tbtotal + gwtotal;
                log.info("关键词搜索--------------开始-------{}---------{}----------{}----------{}--------{}----------{}",totalNum,gbtotal,hbtotal,dbtotal,tbtotal,gwtotal);
                rspData.setGbTotal(gbtotal);
                rspData.setDbTotal(dbtotal);
                rspData.setHbTotal(hbtotal);
                rspData.setTbTotal(tbtotal);
                rspData.setGwTotal(gwtotal);
                rspData.setTotal(totalNum);

                // 2. 定义分类顺序和总数（用于定位目标分类）
                // 数组顺序：GB → HB → DB → TB → GW（可按业务调整）
                String[] categories = {"gb", "hb", "db", "tb", "gw"};
                long[] categoryTotals = {gbtotal, hbtotal, dbtotal, tbtotal, gwtotal};
                int accumulated = 0; // 累计总数（全局索引）

                // 3. 遍历分类，定位当前页数据所在的目标分类
                for (int i = 0; i < categories.length; i++) {
                    String cat = categories[i];
                    long catTotal = categoryTotals[i];
                    if (catTotal <= 0) continue; // 跳过无数据分类

                    // 当前分类的全局范围：[accumulated, accumulated + catTotal)
                    int catStart = accumulated;
                    int catEnd = accumulated + (int) catTotal;

                    // 判断是否与当前页范围 [from, from+pageSize) 有交集
                    if (catEnd <= from) {
                        // 分类在当前页之前，累加后跳过
                        accumulated = catEnd;
                        continue;
                    }
                    if (catStart >= from + pageSize) {
                        // 分类在当前页之后，后续无需处理
                        break;
                    }

                    // 4. 计算当前分类内的查询参数（精准分页）
                    int targetFrom = Math.max(0, from - catStart); // 分类内起始索引
                    int targetSize = Math.min((int) catTotal - targetFrom, from + pageSize - catStart); // 查询长度

                    // 5. 调用现有方法，查询该分类的精准分页数据（传入 targetFrom 和 targetSize）
                    SearchResponse<Object> targetResponse = null;
                    switch (cat) {
                        case "gb":
                            targetResponse = searchGbTotalK(byDb15, targetSize, targetFrom, highlightList);
                            break;
                        case "hb":
                            targetResponse = searchHbTotalK(byDb15, targetSize, targetFrom, highlightList);
                            break;
                        case "db":
                            targetResponse = searchDbTotalK(byDb15, targetSize, targetFrom, highlightList);
                            break;
                        case "tb":
                            targetResponse = searchTbTotalK(byDb15, targetSize, targetFrom, highlightList);
                            break;
                        case "gw":
                            targetResponse = searchGwTotalK(byDb15, targetSize, targetFrom, highlightList);
                            break;
                    }
                    // 6. 组装数据到结果列表（复用原有逻辑）
                    if (targetResponse != null && targetResponse.hits() != null) {
                        for (Hit<Object> hit : targetResponse.hits().hits()) {
                            Map<String, Object> map = new HashMap<>();
                            Map<String, Object> source = (Map<String, Object>) hit.source();
                            Map<String, List<String>> highlightMap = hit.highlight();

                            // 处理高亮字段
                            map.put("a100", highlightMap.containsKey("a100") ? highlightMap.get("a100").get(0) : source.get("a100"));
                            map.put("a298", highlightMap.containsKey("a298") ? highlightMap.get("a298").get(0) : source.get("a298"));
                            map.put("a302", highlightMap.containsKey("a302") ? highlightMap.get("a302").get(0) : source.get("a302"));
                            map.put("a895", highlightMap.containsKey("a895") ? highlightMap.get("a895").get(0) : source.get("a895"));
                            map.put("a209", highlightMap.containsKey("a209") ? highlightMap.get("a209").get(0) : source.get("a209"));

                            // 其他字段
                            map.put("index", hit.index());
                            map.put("id", hit.id());
                            map.put("a101", source.get("a101"));
                            map.put("a102", source.get("a102"));
                            map.put("a200", source.get("a200"));
                            map.put("a205", source.get("a205"));
                            map.put("a206", source.get("a206"));
                            map.put("a330", source.get("a330"));
                            map.put("a331", source.get("a331"));
                            map.put("a461", source.get("a461"));
                            map.put("a462", source.get("a462"));
                            map.put("a825", source.get("a825"));
                            map.put("a826", source.get("a826"));
                            map.put("a310", source.get("a310"));
                            map.put("filePath", source.get("filePath"));
                            map.put("table_name", source.get("table_name"));

                            dataList.add(map);
                            if (dataList.size() >= pageSize) {
                                break; // 数据取满，终止循环
                            }
                        }
                    }

                    accumulated = catEnd;
                    if (dataList.size() >= pageSize) {
                        break; // 所有目标分类查询完毕，终止循环
                    }
                }


            }else{
                SearchResponse<Object> response = esContext.searchIndex(indexList, b, from, pageSize, sort.getSorts(), highLight.getHighlight());
                if (response != null) {
                    for (Hit<Object> hit : response.hits().hits()) {
                        Map<String, Object> map = new HashMap<>();
                        Map<String, Object> source = (Map<String, Object>) hit.source();
                        Map<String, List<String>> highlight = hit.highlight();
                        List<String> a100List = highlight.get("a100");
                        List<String> a298List = highlight.get("a298");
                        List<String> a302List = highlight.get("a302");
                        List<String> a895List = highlight.get("a895");
                        List<String> a209List = highlight.get("a209");

                        map.put("a100", highlight.get("a100"));
                        map.put("a298", highlight.get("a298"));
                        map.put("a302", highlight.get("a302"));
                        map.put("a895", highlight.get("a895"));
                        map.put("a209", highlight.get("a209"));

                        if (a100List != null) {
                            map.put("a100", a100List.get(0));
                        }else{
                            map.put("a100", source.get("a100"));
                        }
                        if (a298List != null) {
                            map.put("a298", a298List.get(0));
                        }else{
                            map.put("a298", source.get("a298"));
                        }
                        if (a302List != null) {
                            map.put("a302", a302List.get(0));
                        }else{
                            map.put("a302", source.get("a302"));
                        }
                        if (a895List != null) {
                            map.put("a895", a895List.get(0));
                        }else{
                            map.put("a895", source.get("a895"));
                        }
                        if (a209List != null) {
                            map.put("a209", a209List.get(0));
                        }else{
                            map.put("a209", source.get("a209"));
                        }

                        String index = hit.index();
                        map.put("index", index);
                        map.put("id", hit.id());

                        map.put("a101", source.get("a101"));
                        map.put("a102", source.get("a102"));
                        map.put("a200", source.get("a200"));
                        map.put("a205", source.get("a205"));
                        map.put("a206", source.get("a206"));
                        map.put("a895", source.get("a895"));
                        map.put("a330", source.get("a330"));
                        map.put("a331", source.get("a331"));
                        map.put("a461", source.get("a461"));
                        map.put("a462", source.get("a462"));
                        map.put("a825", source.get("a825"));
                        map.put("a826", source.get("a826"));
                        map.put("a310", source.get("a310"));
                        map.put("filePath", source.get("filePath"));
                        map.put("table_name", source.get("table_name"));
                        dataList.add(map);
                    }
                }
                rspData.setTotal(response == null ? 0 : response.hits().total().value());
            }
            //拼凑
            rspData.setRows(dataList);
            return rspData;
        } catch (Exception e) {
            e.printStackTrace();
            return rspData;
        } finally {
        }
    }



    public long searchGbAllB(ByDb15 byDb15) {
        String keyNum = byDb15.getA298();
        keyNum = addSlashTIfGbWithNumber(keyNum);
        String standardType = byDb15.getA850();
        String a200 = byDb15.getA200();
        String a101 = byDb15.getA101();


        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(200);
        rspData.setMsg("查询成功");
        rspData.setRows(Collections.emptyList());


        try {
            List<Map<String, Object>> dataList = new ArrayList<>();
            ESBuilder b = ESBuilder.builder().build();
            List<String> indexList = new ArrayList<>();
            indexList.add("new_standard_all");
            List<String> highlightList = new ArrayList<>();
            highlightList.add("a100");
            highlightList.add("a298");
            if(StringUtils.isNotEmpty(keyNum) ){
                ESBuilder build = ESBuilder.builder().build();
                build.buildBoolMust("a100", keyNum);
                build.buildQueryMutiMath(CommonConstants.searchFieldA100A298, keyNum);
                b.buildBoolMust(build.getQuery());
            }
            ESBuilder build = ESBuilder.builder().build();
            build.buildQueryMutiMath(CommonConstants.searchFieldA850, "GB");
            b.buildBoolMust(build.getQuery());

            SearchResponse<Object> response = esContext.searchIndex(indexList, b, 0, 10,null,null);
            // 数据量太大 log.info("查询到结果:{}", response ==null?"":JSON.toJSONString(response.hits().hits().stream().map(Hit::source).collect(Collectors.toList())));
//            rspData.setRows(dataList);
//            rspData.setTotal(response == null ? 0 : response.hits().total().value());
            return response == null ? 0 : response.hits().total().value();
        } catch (Exception e) {
            e.printStackTrace();
            return 0L;
        } finally {
        }
    }

    public long searchHbAllB(ByDb15 byDb15) {
        String keyNum = byDb15.getA298();
        keyNum = addSlashTIfGbWithNumber(keyNum);
        String standardType = byDb15.getA850();
        String a200 = byDb15.getA200();
        String a101 = byDb15.getA101();


        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(200);
        rspData.setMsg("查询成功");
        rspData.setRows(Collections.emptyList());


        try {
            List<Map<String, Object>> dataList = new ArrayList<>();
            ESBuilder b = ESBuilder.builder().build();
            List<String> indexList = new ArrayList<>();
            indexList.add("new_standard_all");
            List<String> highlightList = new ArrayList<>();
            highlightList.add("a100");
            highlightList.add("a298");
            if(StringUtils.isNotEmpty(keyNum) ){
                ESBuilder build = ESBuilder.builder().build();
                build.buildBoolMust("a100", keyNum);
                build.buildQueryMutiMath(CommonConstants.searchFieldA100A298, keyNum);
                b.buildBoolMust(build.getQuery());

            }
            ESBuilder build = ESBuilder.builder().build();
            build.buildQueryMutiMath(CommonConstants.searchFieldA850, CommonConstants.hbString);
            b.buildBoolMust(build.getQuery());


            SearchResponse<Object> response = esContext.searchIndex(indexList, b, 0, 10,null,null);
            // 数据量太大 log.info("查询到结果:{}", response ==null?"":JSON.toJSONString(response.hits().hits().stream().map(Hit::source).collect(Collectors.toList())));

            return response == null ? 0 : response.hits().total().value();
        } catch (Exception e) {
            e.printStackTrace();
            return 0L;
        } finally {
        }
    }

    public long searchDbAllB(ByDb15 byDb15) {
        String keyNum = byDb15.getA298();
        keyNum = addSlashTIfGbWithNumber(keyNum);
        String standardType = byDb15.getA850();
        String a200 = byDb15.getA200();
        String a101 = byDb15.getA101();


        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(200);
        rspData.setMsg("查询成功");
        rspData.setRows(Collections.emptyList());


        try {
            List<Map<String, Object>> dataList = new ArrayList<>();
            ESBuilder b = ESBuilder.builder().build();
            List<String> indexList = new ArrayList<>();
            indexList.add("new_standard_all");
            List<String> highlightList = new ArrayList<>();
            highlightList.add("a100");
            highlightList.add("a298");
            if(StringUtils.isNotEmpty(keyNum) ){
                ESBuilder build = ESBuilder.builder().build();
                build.buildBoolMust("a100", keyNum);
                build.buildQueryMutiMath(CommonConstants.searchFieldA100A298, keyNum);
//                build.buildQueryMutiMath(CommonConstants.searchFieldA850, "DB");
                b.buildBoolMust(build.getQuery());
            }


            ESBuilder builda = ESBuilder.builder().build();
            builda.buildQueryMutiMath(CommonConstants.searchFieldA850, "DB");
            b.buildBoolMust(builda.getQuery());

            SearchResponse<Object> response = esContext.searchIndex(indexList, b, 0, 10,null, null);
            // 数据量太大 log.info("查询到结果:{}", response ==null?"":JSON.toJSONString(response.hits().hits().stream().map(Hit::source).collect(Collectors.toList())));

//            rspData.setTotal(response == null ? 0 : response.hits().total().value());
            return response == null ? 0 : response.hits().total().value();
        } catch (Exception e) {
            e.printStackTrace();
            return 0L;
        } finally {
        }
    }

    public long searchTbAllB(ByDb15 byDb15) {
        String keyNum = byDb15.getA298();
        keyNum = addSlashTIfGbWithNumber(keyNum);
        String standardType = byDb15.getA850();
        String a200 = byDb15.getA200();
        String a101 = byDb15.getA101();


        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(200);
        rspData.setMsg("查询成功");
        rspData.setRows(Collections.emptyList());


        try {
            List<Map<String, Object>> dataList = new ArrayList<>();
            ESBuilder b = ESBuilder.builder().build();
            List<String> indexList = new ArrayList<>();
            indexList.add("new_standard_all");
            List<String> highlightList = new ArrayList<>();
            highlightList.add("a100");
            highlightList.add("a298");
            if(StringUtils.isNotEmpty(keyNum) ){
                ESBuilder build = ESBuilder.builder().build();
                build.buildBoolMust("a100", keyNum);
                build.buildQueryMutiMath(CommonConstants.searchFieldA100A298, keyNum);
                b.buildBoolMust(build.getQuery());
            }
            ESBuilder build = ESBuilder.builder().build();
            build.buildQueryMutiMath(CommonConstants.searchFieldA850, "T/");
            b.buildBoolMust(build.getQuery());

            SearchResponse<Object> response = esContext.searchIndex(indexList, b, 0, 10,null, null);
            // 数据量太大 log.info("查询到结果:{}", response ==null?"":JSON.toJSONString(response.hits().hits().stream().map(Hit::source).collect(Collectors.toList())));

//            rspData.setTotal(response == null ? 0 : response.hits().total().value());
            return response == null ? 0 : response.hits().total().value();
        } catch (Exception e) {
            e.printStackTrace();
            return 0L;
        } finally {
        }
    }

    public long searchGwAllB(ByDb15 byDb15) {
        String keyNum = byDb15.getA298();
        keyNum = addSlashTIfGbWithNumber(keyNum);
        String standardType = byDb15.getA850();
        String a200 = byDb15.getA200();
        String a101 = byDb15.getA101();


        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(200);
        rspData.setMsg("查询成功");
        rspData.setRows(Collections.emptyList());


        try {
            List<Map<String, Object>> dataList = new ArrayList<>();
            ESBuilder b = ESBuilder.builder().build();
            List<String> indexList = new ArrayList<>();
            indexList.add("new_standard_all");
            List<String> highlightList = new ArrayList<>();
            highlightList.add("a100");
            highlightList.add("a298");
            if(StringUtils.isNotEmpty(keyNum) ){
                ESBuilder build = ESBuilder.builder().build();
                build.buildBoolMust("a100", keyNum);
                build.buildQueryMutiMath(CommonConstants.searchFieldA100A298, keyNum);
                b.buildBoolMust(build.getQuery());
            }
            ESBuilder build = ESBuilder.builder().build();
            build.buildQueryMutiMath(CommonConstants.searchFieldA850, CommonConstants.gwString);
            b.buildBoolMust(build.getQuery());

            SearchResponse<Object> response = esContext.searchIndex(indexList, b, 0, 10,null, null);
            // 数据量太大 log.info("查询到结果:{}", response ==null?"":JSON.toJSONString(response.hits().hits().stream().map(Hit::source).collect(Collectors.toList())));


            return response == null ? 0 : response.hits().total().value();
        } catch (Exception e) {
            e.printStackTrace();
            return 0L;
        } finally {
        }
    }

    @Override
    public List<ByDb15> exportFile(ByDb15 byDb15) {
        //开线程
        //建立导出任务 状态为0 未完成
        ByExportTask byExportTask = new ByExportTask();
        byExportTask.setStatus(CommonConstants.TASK_NOCOMPLETE.toString());
        byExportTask.setCareteBy("");
        byExportTask.setFilePath("");
        byExportTask.setCreatTime(DateUtils.getNowDate());
        byExportTask.setKeyword(byDb15.getA298());
        byExportTask.setTaskType(2L);
        byExportTask.setStandardStatus(byDb15.getA200());
        byExportTask.setStandardType(byDb15.getA850());
        byExportTask.setYearNum(byDb15.getA101());
        byExportTaskService.insertByExportTask(byExportTask);
        AtomicInteger allCount = new AtomicInteger(0);// 记录处理总条数
        AtomicInteger failCount = new AtomicInteger(0);// 记录失败总条数
        AtomicInteger successCount = new AtomicInteger(0);// 记录成功总条数
        ByExportThreadFileTask byThreadTask = new ByExportThreadFileTask("1",allCount, failCount, successCount);
        byThreadTask.setTaskId(Integer.parseInt(byExportTask.getId().toString()));
        byThreadTask.setKeyword(byDb15.getA298());
        byThreadTask.setA101(byDb15.getA101());
        byThreadTask.setA200(byDb15.getA200());
        byThreadTask.setStandardType(byDb15.getA850());
        taskExecutor.execute(byThreadTask);
        return null;
    }

    @Override
    public List<ByDb15> export(ByDb15 byDb15) {
        //开线程
        //建立导出任务 状态为0 未完成
        ByExportTask byExportTask = new ByExportTask();
        byExportTask.setStatus(CommonConstants.TASK_NOCOMPLETE.toString());
        byExportTask.setCareteBy("");
        byExportTask.setFilePath("");
        byExportTask.setCreatTime(DateUtils.getNowDate());
        byExportTask.setKeyword(byDb15.getA298());
        byExportTask.setTaskType(1L);
        byExportTask.setStandardStatus(byDb15.getA200());
        byExportTask.setStandardType(byDb15.getA850());
        byExportTask.setYearNum(byDb15.getA101());
        byExportTaskService.insertByExportTask(byExportTask);
        AtomicInteger allCount = new AtomicInteger(0);// 记录处理总条数
        AtomicInteger failCount = new AtomicInteger(0);// 记录失败总条数
        AtomicInteger successCount = new AtomicInteger(0);// 记录成功总条数
        ByExportThreadTask byThreadTask = new ByExportThreadTask("1",allCount, failCount, successCount);
        byThreadTask.setTaskId(Integer.parseInt(byExportTask.getId().toString()));
        byThreadTask.setKeyword(byDb15.getA298());
        byThreadTask.setA101(byDb15.getA101());
        byThreadTask.setA200(byDb15.getA200());
        byThreadTask.setStandardType(byDb15.getA850());
        taskExecutor.execute(byThreadTask);
        return null;
    }


    public static <T> T mapToEntity(Map<String, Object> dataMap, Class<T> clazz) {
        try {
            T obj = clazz.newInstance(); // 创建实体对象实例
            for (Map.Entry<String, Object> entityKey : dataMap.entrySet()) {
                String fieldName = entityKey.getKey(); // 获取字段名
                Field field = clazz.getDeclaredField(fieldName); // 获取字段对象
                if (!field.isAccessible()) { // 如果字段不可访问，‌则设置为可访问
                    field.setAccessible(true);
                }
                field.set(obj, entityKey.getValue()); // 设置字段值
            }
            return obj; // 返回实体对象实例
        } catch (Exception e) { // 异常处理逻辑省略...
            log.info(e.getMessage());
            return null;
        }
    }

    @Override
    public TableDataInfo mutilSearch(ByDb15 byDb15,Integer pageNum,Integer pageSize) {
        String a101 =  byDb15.getA101();
        if(StringUtils.isNotEmpty(a101) && a101.length() > 5){
            String yearStr = a101.substring(0, 4);
            // 转换为整数并加1
            int year = Integer.parseInt(yearStr);
            int nextYear = year + 1;
            byDb15.setA101(nextYear+"");
        }
        //处理标准号 gb默认加T
        String a100 = byDb15.getA100();

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(200);
        rspData.setMsg("查询成功");
        rspData.setRows(Collections.emptyList());
        try {
            List<Map<String, Object>> dataList = new ArrayList<>();
            List<String> highlightList = new ArrayList<>();
            highlightList.add("a100");
            ESBuilder b = ESBuilder.builder().build();
            if (StringUtils.isNotNull(byDb15.getA100()) && StringUtils.isNotEmpty(byDb15.getA100())) {
                ESBuilder build = ESBuilder.builder().build();
//                build.buildBoolShouldWildCard("a100", byDb15.getA100());
                String a100S = addSlashTIfGbWithNumber(byDb15.getA100());
                build.buildQueryMutiMath(CommonConstants.searchFieldA100, a100S);
                b.buildBoolMust(build.getQuery());
//                highlightList.add("a100");
            }
            if (StringUtils.isNotNull(byDb15.getA298()) && StringUtils.isNotEmpty(byDb15.getA298())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA298, byDb15.getA298());
                b.buildBoolMust(build.getQuery());
                highlightList.add("a298");
            }
            if (StringUtils.isNotNull(byDb15.getA302()) && StringUtils.isNotEmpty(byDb15.getA302())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA302, byDb15.getA302());
                b.buildBoolMust(build.getQuery());
                highlightList.add("a302");
            }
            if (StringUtils.isNotNull(byDb15.getA895()) && StringUtils.isNotEmpty(byDb15.getA895())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA895, byDb15.getA895());
                b.buildBoolMust(build.getQuery());
                highlightList.add("a895");
            }
            if (StringUtils.isNotNull(byDb15.getA209()) && StringUtils.isNotEmpty(byDb15.getA209())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA209, byDb15.getA209());
                b.buildBoolMust(build.getQuery());
                highlightList.add("a209");
            }
            if (StringUtils.isNotNull(byDb15.getA825()) && StringUtils.isNotEmpty(byDb15.getA825())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA825, byDb15.getA825());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA826()) && StringUtils.isNotEmpty(byDb15.getA826())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA826, byDb15.getA826());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA200()) && StringUtils.isNotEmpty(byDb15.getA200())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA200, byDb15.getA200());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA850()) && StringUtils.isNotEmpty(byDb15.getA850())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA100, byDb15.getA850());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getCategory()) && StringUtils.isNotEmpty(byDb15.getCategory())) {
                String category = byDb15.getCategory();
                if(category.equals("all")){

                }else if(category.equals("gb")){
                    ESBuilder build = ESBuilder.builder().build();
                    build.buildQueryMutiMath(CommonConstants.searchFieldA850, "GB");
                    b.buildBoolMust(build.getQuery());
                }else if(category.equals("hb")){
                    ESBuilder build = ESBuilder.builder().build();
                    build.buildQueryMutiMath(CommonConstants.searchFieldA850, CommonConstants.hbString);
                    b.buildBoolMust(build.getQuery());
                }else if(category.equals("db")){
                    ESBuilder build = ESBuilder.builder().build();
                    build.buildQueryMutiMath(CommonConstants.searchFieldA850, "DB");
                    b.buildBoolMust(build.getQuery());
                }else if(category.equals("tb")){
                    ESBuilder build = ESBuilder.builder().build();
                    build.buildQueryMutiMath(CommonConstants.searchFieldA850, "T/");
                    b.buildBoolMust(build.getQuery());
                }else if(category.equals("gw")){
                    ESBuilder build = ESBuilder.builder().build();
                    build.buildQueryMutiMath(CommonConstants.searchFieldA850, CommonConstants.gwString);
                    b.buildBoolMust(build.getQuery());
                }
            }
//            b.buildGroupBy(CommonConstants.searchFieldA850String,false);
            if (StringUtils.isNotNull(byDb15.getA101()) && StringUtils.isNotEmpty(byDb15.getA101())) {
//                ESBuilder build = ESBuilder.builder().build();
                // 构建范围查询（添加到 filterQuerys 列表）
                String year = byDb15.getA101();
                String startDate = year + "-01-01";
                String endDate = year + "-12-31";
                b.buildQueryListRange(CommonConstants.searchFieldA101, "gt", startDate);
                b.buildQueryListRange(CommonConstants.searchFieldA101, "lt", endDate);
                // 从 filterQuerys 中获取查询条件（注意：filterQuerys 可能为 null 或空
            }

//            if (StringUtils.isNotEmpty(syscode)) {
//                b.buildBoolMustWildCard("class_system", syscode);
//            }

            Integer from = (pageNum - 1) * pageSize;
//            List<String> indexList = getIndexByDatabaseCode(code, replace);
//            if (indexList.isEmpty()) {
//                return rspData;
//            }
            List<String> indexList = new ArrayList<>();
            indexList.add(CommonConstants.INDEX_PREFIX);
            ESBuilder highLight = ESBuilder.builder().build().buildHighLight("<span style=\"color:red\">", "</span>", 5, 50, highlightList);
//            ESBuilder sort = ESBuilder.builder().build().buildSort("create_time", "desc", false);
            ESBuilder sort = ESBuilder.builder().build().buildSort("a101", "desc", false);

            SearchResponse<Object> response = esContext.searchIndex(indexList, b, from, pageSize, sort.getSorts(), highLight.getHighlight());
            // 数据量太大 log.info("查询到结果:{}", response ==null?"":JSON.toJSONString(response.hits().hits().stream().map(Hit::source).collect(Collectors.toList())));
            if (response != null) {
                for (Hit<Object> hit : response.hits().hits()) {
                    Map<String, Object> map = new HashMap<>();
                    Map<String, Object> source = (Map<String, Object>) hit.source();
                    Map<String, List<String>> highlight = hit.highlight();
                    List<String> a100List = highlight.get("a100");
                    List<String> a298List = highlight.get("a298");
                    List<String> a302List = highlight.get("a302");
                    List<String> a895List = highlight.get("a895");
                    List<String> a209List = highlight.get("a209");

                    map.put("a100", highlight.get("a100"));
                    map.put("a298", highlight.get("a298"));
                    map.put("a302", highlight.get("a302"));
                    map.put("a895", highlight.get("a895"));
                    map.put("a209", highlight.get("a209"));

//                    List<String> abstractList = highlight.get("abstract");
//                    List<String> keywordList = highlight.get("keyword");
//                    map.put("title", source.get("title"));
//                    map.put("abstract", source.get("abstract"));
//                    map.put("keyword", source.get("keyword"));
//                    if (StringUtils.isNotEmpty(syscode)) {

                    if (a100List != null) {
                        map.put("a100", a100List.get(0));
                    }else{
                        map.put("a100", source.get("a100"));
                    }
                    if (a298List != null) {
                        map.put("a298", a298List.get(0));
                    }else{
                        map.put("a298", source.get("a298"));
                    }
                    if (a302List != null) {
                        map.put("a302", a302List.get(0));
                    }else{
                        map.put("a302", source.get("a302"));
                    }
                    if (a895List != null) {
                        map.put("a895", a895List.get(0));
                    }else{
                        map.put("a895", source.get("a895"));
                    }
                    if (a209List != null) {
                        map.put("a209", a209List.get(0));
                    }else{
                        map.put("a209", source.get("a209"));
                    }

//                        if (abstractList != null) {
//                            map.put("abstract", String.join(",", abstractList));
//                        }
//                        if (keywordList != null) {
//                            map.put("keyword", String.join(",", keywordList));
//                        }
//                    }
//                    map.put("createTime", source.get("create_time"));
                    String index = hit.index();
                    map.put("index", index);
                    map.put("id", hit.id());


                    map.put("a101", source.get("a101"));
//                    if(){
//
//                    }
                    map.put("a102", source.get("a102"));
                    map.put("a200", source.get("a200"));
                    map.put("a205", source.get("a205"));
                    map.put("a206", source.get("a206"));
                    map.put("a895", source.get("a895"));
                    map.put("a330", source.get("a330"));
                    map.put("a331", source.get("a331"));
                    map.put("a461", source.get("a461"));
                    map.put("a462", source.get("a462"));
                    map.put("a825", source.get("a825"));
                    map.put("a826", source.get("a826"));
                    map.put("a310", source.get("a310"));
                    map.put("filePath", source.get("filePath"));
                    map.put("table_name", source.get("table_name"));




//                    List<Map<String, String>> fileList = dataContext.excuteQueryList("select filelinkurl,filesize from " + index.replace(CommonConstants.INDEX_PREFIX, "") + "_metadata_file where recordid = " + hit.id());
//                    if (!fileList.isEmpty()) {
//                        map.put("fileUrl", fileList.get(0).get("filelinkurl"));
//                        map.put("fileSize", fileList.get(0).get("filesize") == null ? 0 : new BigDecimal(fileList.get(0).get("filesize")).divide(new BigDecimal(1024), 0, RoundingMode.HALF_UP));
//                        map.put("fileSizeStr", fileList.get(0).get("filesize") == null ? "0B" : FileUtil.formatFileSize(Long.parseLong(fileList.get(0).get("filesize"))));
//                        if (fileList.get(0).get("filelinkurl").lastIndexOf(".") > 0) {
//                            String docType = fileList.get(0).get("filelinkurl").substring(fileList.get(0).get("filelinkurl").lastIndexOf(".") + 1);
//                            String[] allow = allowDocType.split(",");
//                            map.put("canPreview", Arrays.asList(allow).contains(docType));
//                        }
//                    }
                    dataList.add(map);
                }
            }

//            if (StringUtils.isNotNull(byDb15.getA850()) && StringUtils.isNotEmpty(byDb15.getA850())) {
//                if(){
//
//                }
//            }
            rspData.setRows(dataList);
            rspData.setTotal(response == null ? 0 : response.hits().total().value());
            return rspData;
        } catch (Exception e) {
            e.printStackTrace();
            return rspData;
        } finally {
//            try {
//                userService.saveUserBehaviorOfSearch(keyword);
//            } catch (Exception e) {
//            }
        }
    }

    @Override
    public TableDataInfo mutilSearchFront(ByDb15 byDb15, Integer pageNum, Integer pageSize) {
        String a101 =  byDb15.getA101();
        if(StringUtils.isNotEmpty(a101) && a101.length() > 5){
            String yearStr = a101.substring(0, 4);
            // 转换为整数并加1
            int year = Integer.parseInt(yearStr);
            int nextYear = year + 1;
            byDb15.setA101(nextYear+"");
        }
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(200);
        rspData.setMsg("查询成功");
        rspData.setRows(Collections.emptyList());
        try {
            List<Map<String, Object>> dataList = new ArrayList<>();
            List<String> highlightList = new ArrayList<>();
            highlightList.add("a850");
            ESBuilder b = ESBuilder.builder().build();
            if (StringUtils.isNotNull(byDb15.getA100()) && StringUtils.isNotEmpty(byDb15.getA100())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMathGs(CommonConstants.searchFieldA100, byDb15.getA100());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA298()) && StringUtils.isNotEmpty(byDb15.getA298())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA298, byDb15.getA298());
                b.buildBoolMust(build.getQuery());
                highlightList.add("a298");
            }
            if (StringUtils.isNotNull(byDb15.getA302()) && StringUtils.isNotEmpty(byDb15.getA302())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA302, byDb15.getA302());
                b.buildBoolMust(build.getQuery());
                highlightList.add("a302");
            }
            if (StringUtils.isNotNull(byDb15.getA895()) && StringUtils.isNotEmpty(byDb15.getA895())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA895, byDb15.getA895());
                b.buildBoolMust(build.getQuery());
                highlightList.add("a895");
            }
            if (StringUtils.isNotNull(byDb15.getA209()) && StringUtils.isNotEmpty(byDb15.getA209())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA209, byDb15.getA209());
                b.buildBoolMust(build.getQuery());
                highlightList.add("a209");
            }
            if (StringUtils.isNotNull(byDb15.getA825()) && StringUtils.isNotEmpty(byDb15.getA825())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA825, byDb15.getA825());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA826()) && StringUtils.isNotEmpty(byDb15.getA826())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA826, byDb15.getA826());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA200()) && StringUtils.isNotEmpty(byDb15.getA200())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA200, byDb15.getA200());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA850()) && StringUtils.isNotEmpty(byDb15.getA850())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA850, byDb15.getA850());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getCategory()) && StringUtils.isNotEmpty(byDb15.getCategory())) {
                String category = byDb15.getCategory();
                if(category.equals("all")){
//                    ESBuilder build = ESBuilder.builder().build();
//                    build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.totalString);
//                    b.buildBoolMust(build.getQuery());
                    ESBuilder build = ESBuilder.builder().build();
                    build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.totalString);
                    b.buildBoolMust(build.getQuery());
                }else if(category.equals("gb")){
                    ESBuilder build = ESBuilder.builder().build();
                    build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.gbString);
                    b.buildBoolMust(build.getQuery());
                }else if(category.equals("hb")){
                    ESBuilder build = ESBuilder.builder().build();
                    build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.hbString);
                    b.buildBoolMust(build.getQuery());
                }else if(category.equals("db")){
                    ESBuilder build = ESBuilder.builder().build();
                    build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.dbString);
                    b.buildBoolMust(build.getQuery());
                }else if(category.equals("tb")){
                    ESBuilder build = ESBuilder.builder().build();
                    build.buildQueryMutiMath850bak(CommonConstants.searchFieldA850, CommonConstants.tbString);
                    b.buildBoolMust(build.getQuery());
                }else if(category.equals("gw")){
                    ESBuilder build = ESBuilder.builder().build();
                    build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.gwString);
                    b.buildBoolMust(build.getQuery());
                }
            }
            if (StringUtils.isNotNull(byDb15.getA101()) && StringUtils.isNotEmpty(byDb15.getA101())) {
                String year = byDb15.getA101();
                String startDate = year + "-01-01";
                String endDate = year + "-12-31";
                b.buildQueryListRange(CommonConstants.searchFieldA101, "gte", startDate);
                b.buildQueryListRange(CommonConstants.searchFieldA101, "lte", endDate);
            }

            Integer from = (pageNum - 1) * pageSize;
            List<String> indexList = new ArrayList<>();
            indexList.add(CommonConstants.INDEX_PREFIX);
            ESBuilder highLight = ESBuilder.builder().build().buildHighLight("<span style=\"color:red\">", "</span>", 5, 50, highlightList);
            ESBuilder sort = ESBuilder.builder().build().buildSort("a101", "desc", false);
//            SearchResponse<Object> response = esContext.searchIndex(indexList, b, from, pageSize, sort.getSorts(), highLight.getHighlight());

            // 数据量太大 log.info("查询到结果:{}", response ==null?"":JSON.toJSONString(response.hits().hits().stream().map(Hit::source).collect(Collectors.toList())));

            long totalNum = 0L;
            String category = byDb15.getCategory();
            if(category.equals("all")){
                //拼凑list
//                List<SearchResponse<Object>> totalList = new ArrayList();

                SearchResponse<Object> gbres = searchGbTotal(byDb15,0,0,highlightList);
                SearchResponse<Object> hbres = searchHbTotal(byDb15,0,0,highlightList);
                SearchResponse<Object> dbres = searchDbTotal(byDb15,0,0,highlightList);
                SearchResponse<Object> tbres = searchTbTotal(byDb15,0,0,highlightList);
                SearchResponse<Object> gwres = searchGwTotal(byDb15,0,0,highlightList);
//                totalList.add(gbres);
//                totalList.add(hbres);
//                totalList.add(dbres);
//                totalList.add(tbres);
//                totalList.add(gwres);

//                long zhTotal = searchAllTotal(byDb15,pageSize);
                long gbtotal = gbres == null ? 0 : gbres.hits().total().value();
                long hbtotal = hbres == null ? 0 : hbres.hits().total().value();
                long dbtotal = dbres == null ? 0 : dbres.hits().total().value();
                long tbtotal = tbres == null ? 0 : tbres.hits().total().value();
                long gwtotal = gwres == null ? 0 : gwres.hits().total().value();
                totalNum = gbtotal + hbtotal + dbtotal + tbtotal + gwtotal;
                rspData.setGbTotal(gbtotal);
                rspData.setDbTotal(dbtotal);
                rspData.setHbTotal(hbtotal);
                rspData.setTbTotal(tbtotal);
                rspData.setGwTotal(gwtotal);
//                rspData.setZhTotal(zhTotal);
                rspData.setTotal(totalNum);

                // 2. 定义分类顺序和总数（用于定位目标分类）
                // 数组顺序：GB → HB → DB → TB → GW（可按业务调整）
                String[] categories = {"gb", "hb", "db", "tb", "gw"};
                long[] categoryTotals = {gbtotal, hbtotal, dbtotal, tbtotal, gwtotal};
                int accumulated = 0; // 累计总数（全局索引）

                // 3. 遍历分类，定位当前页数据所在的目标分类
                for (int i = 0; i < categories.length; i++) {
                    String cat = categories[i];
                    long catTotal = categoryTotals[i];
                    if (catTotal <= 0) continue; // 跳过无数据分类

                    // 当前分类的全局范围：[accumulated, accumulated + catTotal)
                    int catStart = accumulated;
                    int catEnd = accumulated + (int) catTotal;

                    // 判断是否与当前页范围 [from, from+pageSize) 有交集
                    if (catEnd <= from) {
                        // 分类在当前页之前，累加后跳过
                        accumulated = catEnd;
                        continue;
                    }
                    if (catStart >= from + pageSize) {
                        // 分类在当前页之后，后续无需处理
                        break;
                    }

                    // 4. 计算当前分类内的查询参数（精准分页）
                    int targetFrom = Math.max(0, from - catStart); // 分类内起始索引
                    int targetSize = Math.min((int) catTotal - targetFrom, from + pageSize - catStart); // 查询长度

                    // 5. 调用现有方法，查询该分类的精准分页数据（传入 targetFrom 和 targetSize）
                    SearchResponse<Object> targetResponse = null;
                    switch (cat) {
                        case "gb":
                            targetResponse = searchGbTotal(byDb15, targetSize, targetFrom, highlightList);
                            break;
                        case "hb":
                            targetResponse = searchHbTotal(byDb15, targetSize, targetFrom, highlightList);
                            break;
                        case "db":
                            targetResponse = searchDbTotal(byDb15, targetSize, targetFrom, highlightList);
                            break;
                        case "tb":
                            targetResponse = searchTbTotal(byDb15, targetSize, targetFrom, highlightList);
                            break;
                        case "gw":
                            targetResponse = searchGwTotal(byDb15, targetSize, targetFrom, highlightList);
                            break;
                    }
                    // 6. 组装数据到结果列表（复用原有逻辑）
                    if (targetResponse != null && targetResponse.hits() != null) {
                        for (Hit<Object> hit : targetResponse.hits().hits()) {
                            Map<String, Object> map = new HashMap<>();
                            Map<String, Object> source = (Map<String, Object>) hit.source();
                            Map<String, List<String>> highlightMap = hit.highlight();

                            // 处理高亮字段
                            map.put("a100", highlightMap.containsKey("a100") ? highlightMap.get("a100").get(0) : source.get("a100"));
                            map.put("a298", highlightMap.containsKey("a298") ? highlightMap.get("a298").get(0) : source.get("a298"));
                            map.put("a302", highlightMap.containsKey("a302") ? highlightMap.get("a302").get(0) : source.get("a302"));
                            map.put("a895", highlightMap.containsKey("a895") ? highlightMap.get("a895").get(0) : source.get("a895"));
                            map.put("a209", highlightMap.containsKey("a209") ? highlightMap.get("a209").get(0) : source.get("a209"));

                            // 其他字段
                            map.put("index", hit.index());
                            map.put("id", hit.id());
                            map.put("a101", source.get("a101"));
                            map.put("a102", source.get("a102"));
                            map.put("a200", source.get("a200"));
                            map.put("a205", source.get("a205"));
                            map.put("a206", source.get("a206"));
                            map.put("a330", source.get("a330"));
                            map.put("a331", source.get("a331"));
                            map.put("a461", source.get("a461"));
                            map.put("a462", source.get("a462"));
                            map.put("a825", source.get("a825"));
                            map.put("a826", source.get("a826"));
                            map.put("a310", source.get("a310"));
                            map.put("filePath", source.get("filePath"));
                            map.put("table_name", source.get("table_name"));

                            dataList.add(map);
                            if (dataList.size() >= pageSize) {
                                break; // 数据取满，终止循环
                            }
                        }
                    }

                    accumulated = catEnd;
                    if (dataList.size() >= pageSize) {
                        break; // 所有目标分类查询完毕，终止循环
                    }
                }


//                for(SearchResponse<Object> itemObject : totalList){
//                    for (Hit<Object> hit : itemObject.hits().hits()) {
//                        Map<String, Object> map = new HashMap<>();
//                        Map<String, Object> source = (Map<String, Object>) hit.source();
//                        Map<String, List<String>> highlight = hit.highlight();
//                        List<String> a100List = highlight.get("a100");
//                        List<String> a298List = highlight.get("a298");
//                        List<String> a302List = highlight.get("a302");
//                        List<String> a895List = highlight.get("a895");
//                        List<String> a209List = highlight.get("a209");
//
//                        map.put("a100", highlight.get("a100"));
//                        map.put("a298", highlight.get("a298"));
//                        map.put("a302", highlight.get("a302"));
//                        map.put("a895", highlight.get("a895"));
//                        map.put("a209", highlight.get("a209"));
//
//                        if (a100List != null) {
//                            map.put("a100", a100List.get(0));
//                        }else{
//                            map.put("a100", source.get("a100"));
//                        }
//                        if (a298List != null) {
//                            map.put("a298", a298List.get(0));
//                        }else{
//                            map.put("a298", source.get("a298"));
//                        }
//                        if (a302List != null) {
//                            map.put("a302", a302List.get(0));
//                        }else{
//                            map.put("a302", source.get("a302"));
//                        }
//                        if (a895List != null) {
//                            map.put("a895", a895List.get(0));
//                        }else{
//                            map.put("a895", source.get("a895"));
//                        }
//                        if (a209List != null) {
//                            map.put("a209", a209List.get(0));
//                        }else{
//                            map.put("a209", source.get("a209"));
//                        }
//
//                        String index = hit.index();
//                        map.put("index", index);
//                        map.put("id", hit.id());
//
//                        map.put("a101", source.get("a101"));
//                        map.put("a102", source.get("a102"));
//                        map.put("a200", source.get("a200"));
//                        map.put("a205", source.get("a205"));
//                        map.put("a206", source.get("a206"));
//                        map.put("a895", source.get("a895"));
//                        map.put("a330", source.get("a330"));
//                        map.put("a331", source.get("a331"));
//                        map.put("a461", source.get("a461"));
//                        map.put("a462", source.get("a462"));
//                        map.put("a825", source.get("a825"));
//                        map.put("a826", source.get("a826"));
//                        map.put("a310", source.get("a310"));
//                        map.put("filePath", source.get("filePath"));
//                        map.put("table_name", source.get("table_name"));
//                        dataList.add(map);
//                    }
//                }

            }else{
                SearchResponse<Object> response = esContext.searchIndex(indexList, b, from, pageSize, sort.getSorts(), highLight.getHighlight());
                if (response != null) {
                    for (Hit<Object> hit : response.hits().hits()) {
                        Map<String, Object> map = new HashMap<>();
                        Map<String, Object> source = (Map<String, Object>) hit.source();
                        Map<String, List<String>> highlight = hit.highlight();
                        List<String> a100List = highlight.get("a100");
                        List<String> a298List = highlight.get("a298");
                        List<String> a302List = highlight.get("a302");
                        List<String> a895List = highlight.get("a895");
                        List<String> a209List = highlight.get("a209");

                        map.put("a100", highlight.get("a100"));
                        map.put("a298", highlight.get("a298"));
                        map.put("a302", highlight.get("a302"));
                        map.put("a895", highlight.get("a895"));
                        map.put("a209", highlight.get("a209"));

                        if (a100List != null) {
                            map.put("a100", a100List.get(0));
                        }else{
                            map.put("a100", source.get("a100"));
                        }
                        if (a298List != null) {
                            map.put("a298", a298List.get(0));
                        }else{
                            map.put("a298", source.get("a298"));
                        }
                        if (a302List != null) {
                            map.put("a302", a302List.get(0));
                        }else{
                            map.put("a302", source.get("a302"));
                        }
                        if (a895List != null) {
                            map.put("a895", a895List.get(0));
                        }else{
                            map.put("a895", source.get("a895"));
                        }
                        if (a209List != null) {
                            map.put("a209", a209List.get(0));
                        }else{
                            map.put("a209", source.get("a209"));
                        }

                        String index = hit.index();
                        map.put("index", index);
                        map.put("id", hit.id());

                        map.put("a101", source.get("a101"));
                        map.put("a102", source.get("a102"));
                        map.put("a200", source.get("a200"));
                        map.put("a205", source.get("a205"));
                        map.put("a206", source.get("a206"));
                        map.put("a895", source.get("a895"));
                        map.put("a330", source.get("a330"));
                        map.put("a331", source.get("a331"));
                        map.put("a461", source.get("a461"));
                        map.put("a462", source.get("a462"));
                        map.put("a825", source.get("a825"));
                        map.put("a826", source.get("a826"));
                        map.put("a310", source.get("a310"));
                        map.put("filePath", source.get("filePath"));
                        map.put("table_name", source.get("table_name"));
                        dataList.add(map);
                    }
                }
                rspData.setTotal(response == null ? 0 : response.hits().total().value());
            }
            //拼凑
            rspData.setRows(dataList);
//            rspData.setTotal(response == null ? 0 : response.hits().total().value());
//            rspData.setTotal(totalNum);
            return rspData;
        } catch (Exception e) {
            e.printStackTrace();
            return rspData;
        } finally {
//            try {
//                userService.saveUserBehaviorOfSearch(keyword);
//            } catch (Exception e) {
//            }
        }
    }


    public long searchAllTotal(ByDb15 byDb15,Integer pageSize) {
        String a101 =  byDb15.getA101();
        if(StringUtils.isNotEmpty(a101) && a101.length() > 5){
            String yearStr = a101.substring(0, 4);
            // 转换为整数并加1
            int year = Integer.parseInt(yearStr);
            int nextYear = year + 1;
            byDb15.setA101(nextYear+"");
        }
//        byDb15.setA850("GB");
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(200);
        rspData.setMsg("查询成功");
        rspData.setRows(Collections.emptyList());
        try {
            ESBuilder b = ESBuilder.builder().build();
            if (StringUtils.isNotNull(byDb15.getA100()) && StringUtils.isNotEmpty(byDb15.getA100())) {
                ESBuilder build = ESBuilder.builder().build();
//                String a100S = addSlashTIfGbWithNumber(byDb15.getA100());
                build.buildQueryMutiMathGb(CommonConstants.searchFieldA100, byDb15.getA100());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA298()) && StringUtils.isNotEmpty(byDb15.getA298())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA298, byDb15.getA298());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA302()) && StringUtils.isNotEmpty(byDb15.getA302())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA302, byDb15.getA302());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA895()) && StringUtils.isNotEmpty(byDb15.getA895())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA895, byDb15.getA895());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA209()) && StringUtils.isNotEmpty(byDb15.getA209())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA209, byDb15.getA209());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA825()) && StringUtils.isNotEmpty(byDb15.getA825())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA825, byDb15.getA825());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA826()) && StringUtils.isNotEmpty(byDb15.getA826())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA826, byDb15.getA826());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA200()) && StringUtils.isNotEmpty(byDb15.getA200())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA200, byDb15.getA200());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA850()) && StringUtils.isNotEmpty(byDb15.getA850())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA850, byDb15.getA850());
                b.buildBoolMust(build.getQuery());
            }

            if (StringUtils.isNotNull(byDb15.getA101()) && StringUtils.isNotEmpty(byDb15.getA101())) {
                String year = byDb15.getA101();
                String startDate = year + "-01-01";
                String endDate = year + "-12-31";
                b.buildQueryListRange(CommonConstants.searchFieldA101, "gte", startDate);
                b.buildQueryListRange(CommonConstants.searchFieldA101, "lte", endDate);
            }
            ESBuilder build = ESBuilder.builder().build();
            build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.totalString);
            b.buildBoolMust(build.getQuery());
//            b.buildQueryMutiMath(CommonConstants.searchFieldA850, "GB");

            List<String> indexList = new ArrayList<>();
            indexList.add(CommonConstants.INDEX_PREFIX);
            SearchResponse<Object> response = esContext.searchIndex(indexList, b, 0, pageSize,null,null);
            // 数据量太大 log.info("查询到结果:{}", response ==null?"":JSON.toJSONString(response.hits().hits().stream().map(Hit::source).collect(Collectors.toList())));
            return response == null ? 0 : response.hits().total().value();
        }catch (Exception e) {
            e.printStackTrace();
            return 0L;
        } finally {
        }
    }
    //是否切换
    //
    public SearchResponse<Object> searchGbTotal(ByDb15 byDb15,Integer pageSize,Integer from,List<String> highlightList) {
        //            SearchResponse<Object> response = esContext.searchIndex(indexList, b, from, pageSize, sort.getSorts(), highLight.getHighlight());

        String a101 =  byDb15.getA101();
        if(StringUtils.isNotEmpty(a101) && a101.length() > 5){
            String yearStr = a101.substring(0, 4);
            // 转换为整数并加1
            int year = Integer.parseInt(yearStr);
            int nextYear = year + 1;
            byDb15.setA101(nextYear+"");
        }
//        byDb15.setA850("GB");
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(200);
        rspData.setMsg("查询成功");
        rspData.setRows(Collections.emptyList());
        try {
            ESBuilder b = ESBuilder.builder().build();
            if (StringUtils.isNotNull(byDb15.getA100()) && StringUtils.isNotEmpty(byDb15.getA100())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMathGb(CommonConstants.searchFieldA100, byDb15.getA100());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA298()) && StringUtils.isNotEmpty(byDb15.getA298())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA298, byDb15.getA298());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA302()) && StringUtils.isNotEmpty(byDb15.getA302())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA302, byDb15.getA302());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA895()) && StringUtils.isNotEmpty(byDb15.getA895())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA895, byDb15.getA895());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA209()) && StringUtils.isNotEmpty(byDb15.getA209())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA209, byDb15.getA209());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA825()) && StringUtils.isNotEmpty(byDb15.getA825())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA825, byDb15.getA825());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA826()) && StringUtils.isNotEmpty(byDb15.getA826())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA826, byDb15.getA826());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA200()) && StringUtils.isNotEmpty(byDb15.getA200())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA200, byDb15.getA200());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA850()) && StringUtils.isNotEmpty(byDb15.getA850())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA850, byDb15.getA850());
                b.buildBoolMust(build.getQuery());
            }

            if (StringUtils.isNotNull(byDb15.getA101()) && StringUtils.isNotEmpty(byDb15.getA101())) {
                String year = byDb15.getA101();
                String startDate = year + "-01-01";
                String endDate = year + "-12-31";
                b.buildQueryListRange(CommonConstants.searchFieldA101, "gte", startDate);
                b.buildQueryListRange(CommonConstants.searchFieldA101, "lte", endDate);
            }
            ESBuilder build = ESBuilder.builder().build();
            build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.gbString);
            b.buildBoolMust(build.getQuery());
//            b.buildQueryMutiMath(CommonConstants.searchFieldA850, "GB");

            List<String> indexList = new ArrayList<>();
            indexList.add(CommonConstants.INDEX_PREFIX);
            ESBuilder highLight = ESBuilder.builder().build().buildHighLight("<span style=\"color:red\">", "</span>", 5, 50, highlightList);

            ESBuilder sort = ESBuilder.builder().build().buildSort("a101", "desc", false);

            SearchResponse<Object> response = esContext.searchIndex(indexList, b, from, pageSize,sort.getSorts(),highLight.getHighlight());
        // 数据量太大 log.info("查询到结果:{}", response ==null?"":JSON.toJSONString(response.hits().hits().stream().map(Hit::source).collect(Collectors.toList())));
//        return response == null ? 0 : response.hits().total().value();
        return response;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
        }
    }

    public SearchResponse<Object> searchHbTotal(ByDb15 byDb15,Integer pageSize,Integer from,List<String> highlightList) {
        String a101 =  byDb15.getA101();
        if(StringUtils.isNotEmpty(a101) && a101.length() > 5){
            String yearStr = a101.substring(0, 4);
            // 转换为整数并加1
            int year = Integer.parseInt(yearStr);
            int nextYear = year + 1;
            byDb15.setA101(nextYear+"");
        }
//        byDb15.setA850(CommonConstants.hbString);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(200);
        rspData.setMsg("查询成功");
        rspData.setRows(Collections.emptyList());
        try {
            ESBuilder b = ESBuilder.builder().build();
            if (StringUtils.isNotNull(byDb15.getA100()) && StringUtils.isNotEmpty(byDb15.getA100())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMathHb(CommonConstants.searchFieldA100, byDb15.getA100());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA298()) && StringUtils.isNotEmpty(byDb15.getA298())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA298, byDb15.getA298());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA302()) && StringUtils.isNotEmpty(byDb15.getA302())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA302, byDb15.getA302());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA895()) && StringUtils.isNotEmpty(byDb15.getA895())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA895, byDb15.getA895());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA209()) && StringUtils.isNotEmpty(byDb15.getA209())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA209, byDb15.getA209());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA825()) && StringUtils.isNotEmpty(byDb15.getA825())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA825, byDb15.getA825());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA826()) && StringUtils.isNotEmpty(byDb15.getA826())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA826, byDb15.getA826());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA200()) && StringUtils.isNotEmpty(byDb15.getA200())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA200, byDb15.getA200());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA850()) && StringUtils.isNotEmpty(byDb15.getA850())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA850, byDb15.getA850());
                b.buildBoolMust(build.getQuery());
            }

            if (StringUtils.isNotNull(byDb15.getA101()) && StringUtils.isNotEmpty(byDb15.getA101())) {
                String year = byDb15.getA101();
                String startDate = year + "-01-01";
                String endDate = year + "-12-31";
                b.buildQueryListRange(CommonConstants.searchFieldA101, "gte", startDate);
                b.buildQueryListRange(CommonConstants.searchFieldA101, "lte", endDate);
            }

            ESBuilder build = ESBuilder.builder().build();
            build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.hbString);
            b.buildBoolMust(build.getQuery());

            List<String> indexList = new ArrayList<>();
            indexList.add(CommonConstants.INDEX_PREFIX);
            ESBuilder highLight = ESBuilder.builder().build().buildHighLight("<span style=\"color:red\">", "</span>", 5, 50, highlightList);

            ESBuilder sort = ESBuilder.builder().build().buildSort("a101", "desc", false);

            SearchResponse<Object> response = esContext.searchIndex(indexList, b, from, pageSize,sort.getSorts(),highLight.getHighlight());
            // 数据量太大 log.info("查询到结果:{}", response ==null?"":JSON.toJSONString(response.hits().hits().stream().map(Hit::source).collect(Collectors.toList())));
//            return response == null ? 0 : response.hits().total().value();
            return response;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
        }
    }

    public SearchResponse<Object> searchDbTotal(ByDb15 byDb15,Integer pageSize,Integer from,List<String> highlightList) {
        String a101 =  byDb15.getA101();
        if(StringUtils.isNotEmpty(a101) && a101.length() > 5){
            String yearStr = a101.substring(0, 4);
            // 转换为整数并加1
            int year = Integer.parseInt(yearStr);
            int nextYear = year + 1;
            byDb15.setA101(nextYear+"");
        }
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(200);
        rspData.setMsg("查询成功");
        rspData.setRows(Collections.emptyList());
        try {
            ESBuilder b = ESBuilder.builder().build();
            if (StringUtils.isNotNull(byDb15.getA100()) && StringUtils.isNotEmpty(byDb15.getA100())) {
                ESBuilder build = ESBuilder.builder().build();
//                String a100S = addSlashTIfGbWithNumber(byDb15.getA100());
                build.buildQueryMutiMathDb15(CommonConstants.searchFieldA100, byDb15.getA100());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA298()) && StringUtils.isNotEmpty(byDb15.getA298())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA298, byDb15.getA298());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA302()) && StringUtils.isNotEmpty(byDb15.getA302())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA302, byDb15.getA302());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA895()) && StringUtils.isNotEmpty(byDb15.getA895())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA895, byDb15.getA895());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA209()) && StringUtils.isNotEmpty(byDb15.getA209())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA209, byDb15.getA209());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA825()) && StringUtils.isNotEmpty(byDb15.getA825())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA825, byDb15.getA825());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA826()) && StringUtils.isNotEmpty(byDb15.getA826())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA826, byDb15.getA826());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA200()) && StringUtils.isNotEmpty(byDb15.getA200())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA200, byDb15.getA200());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA850()) && StringUtils.isNotEmpty(byDb15.getA850())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA850, byDb15.getA850());
                b.buildBoolMust(build.getQuery());
            }

            if (StringUtils.isNotNull(byDb15.getA101()) && StringUtils.isNotEmpty(byDb15.getA101())) {
                String year = byDb15.getA101();
                String startDate = year + "-01-01";
                String endDate = year + "-12-31";
                b.buildQueryListRange(CommonConstants.searchFieldA101, "gte", startDate);
                b.buildQueryListRange(CommonConstants.searchFieldA101, "lte", endDate);
            }
            ESBuilder build = ESBuilder.builder().build();
            build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.dbString);
            b.buildBoolMust(build.getQuery());


            List<String> indexList = new ArrayList<>();
            indexList.add(CommonConstants.INDEX_PREFIX);
            ESBuilder highLight = ESBuilder.builder().build().buildHighLight("<span style=\"color:red\">", "</span>", 5, 50, highlightList);

            ESBuilder sort = ESBuilder.builder().build().buildSort("a101", "desc", false);

            SearchResponse<Object> response = esContext.searchIndex(indexList, b, from, pageSize,sort.getSorts(),highLight.getHighlight());
            // 数据量太大 log.info("查询到结果:{}", response ==null?"":JSON.toJSONString(response.hits().hits().stream().map(Hit::source).collect(Collectors.toList())));
//            return response == null ? 0 : response.hits().total().value();
            return response;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
        }
    }

    public SearchResponse<Object> searchTbTotal(ByDb15 byDb15,Integer pageSize,Integer from,List<String> highlightList) {
        String a101 =  byDb15.getA101();
        if(StringUtils.isNotEmpty(a101) && a101.length() > 5){
            String yearStr = a101.substring(0, 4);
            // 转换为整数并加1
            int year = Integer.parseInt(yearStr);
            int nextYear = year + 1;
            byDb15.setA101(nextYear+"");
        }
//        byDb15.setA850("GB");
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(200);
        rspData.setMsg("查询成功");
        rspData.setRows(Collections.emptyList());
        try {
            ESBuilder b = ESBuilder.builder().build();
            if (StringUtils.isNotNull(byDb15.getA100()) && StringUtils.isNotEmpty(byDb15.getA100())) {
                ESBuilder build = ESBuilder.builder().build();
//                String a100S = addSlashTIfGbWithNumber(byDb15.getA100());
                build.buildQueryMutiMathDb15(CommonConstants.searchFieldA100, byDb15.getA100());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA298()) && StringUtils.isNotEmpty(byDb15.getA298())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA298, byDb15.getA298());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA302()) && StringUtils.isNotEmpty(byDb15.getA302())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA302, byDb15.getA302());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA895()) && StringUtils.isNotEmpty(byDb15.getA895())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA895, byDb15.getA895());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA209()) && StringUtils.isNotEmpty(byDb15.getA209())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA209, byDb15.getA209());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA825()) && StringUtils.isNotEmpty(byDb15.getA825())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA825, byDb15.getA825());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA826()) && StringUtils.isNotEmpty(byDb15.getA826())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA826, byDb15.getA826());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA200()) && StringUtils.isNotEmpty(byDb15.getA200())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA200, byDb15.getA200());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA850()) && StringUtils.isNotEmpty(byDb15.getA850())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA850, byDb15.getA850());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA101()) && StringUtils.isNotEmpty(byDb15.getA101())) {
                String year = byDb15.getA101();
                String startDate = year + "-01-01";
                String endDate = year + "-12-31";
                b.buildQueryListRange(CommonConstants.searchFieldA101, "gte", startDate);
                b.buildQueryListRange(CommonConstants.searchFieldA101, "lte", endDate);
            }

            ESBuilder build = ESBuilder.builder().build();
            build.buildQueryMutiMath850bak(CommonConstants.searchFieldA850, CommonConstants.tbString);
            b.buildBoolMust(build.getQuery());

            List<String> indexList = new ArrayList<>();
            indexList.add(CommonConstants.INDEX_PREFIX);
            ESBuilder highLight = ESBuilder.builder().build().buildHighLight("<span style=\"color:red\">", "</span>", 5, 50, highlightList);

            ESBuilder sort = ESBuilder.builder().build().buildSort("a101", "desc", false);

            SearchResponse<Object> response = esContext.searchIndex(indexList, b, from, pageSize,sort.getSorts(),highLight.getHighlight());
            // 数据量太大 log.info("查询到结果:{}", response ==null?"":JSON.toJSONString(response.hits().hits().stream().map(Hit::source).collect(Collectors.toList())));
//            return response == null ? 0 : response.hits().total().value();
            return response;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
        }
    }

    public SearchResponse<Object> searchGwTotal(ByDb15 byDb15,Integer pageSize,Integer from,List<String> highlightList) {
        String a101 =  byDb15.getA101();
        if(StringUtils.isNotEmpty(a101) && a101.length() > 5){
            String yearStr = a101.substring(0, 4);
            // 转换为整数并加1
            int year = Integer.parseInt(yearStr);
            int nextYear = year + 1;
            byDb15.setA101(nextYear+"");
        }
//        byDb15.setA850("GB");
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(200);
        rspData.setMsg("查询成功");
        rspData.setRows(Collections.emptyList());
        try {
            ESBuilder b = ESBuilder.builder().build();
            if (StringUtils.isNotNull(byDb15.getA100()) && StringUtils.isNotEmpty(byDb15.getA100())) {
                ESBuilder build = ESBuilder.builder().build();
//                String a100S = addSlashTIfGbWithNumber(byDb15.getA100());
                build.buildQueryMutiMathGb(CommonConstants.searchFieldA100, byDb15.getA100());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA298()) && StringUtils.isNotEmpty(byDb15.getA298())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA298, byDb15.getA298());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA302()) && StringUtils.isNotEmpty(byDb15.getA302())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA302, byDb15.getA302());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA895()) && StringUtils.isNotEmpty(byDb15.getA895())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA895, byDb15.getA895());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA209()) && StringUtils.isNotEmpty(byDb15.getA209())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA209, byDb15.getA209());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA825()) && StringUtils.isNotEmpty(byDb15.getA825())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA825, byDb15.getA825());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA826()) && StringUtils.isNotEmpty(byDb15.getA826())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA826, byDb15.getA826());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA200()) && StringUtils.isNotEmpty(byDb15.getA200())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA200, byDb15.getA200());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA850()) && StringUtils.isNotEmpty(byDb15.getA850())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA850, byDb15.getA850());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA101()) && StringUtils.isNotEmpty(byDb15.getA101())) {
                String year = byDb15.getA101();
                String startDate = year + "-01-01";
                String endDate = year + "-12-31";
                b.buildQueryListRange(CommonConstants.searchFieldA101, "gte", startDate);
                b.buildQueryListRange(CommonConstants.searchFieldA101, "lte", endDate);
            }

            ESBuilder build = ESBuilder.builder().build();
            build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.gwString);
            b.buildBoolMust(build.getQuery());

//            b.buildBoolMust(CommonConstants.searchFieldA850String, CommonConstants.gwString);


            List<String> indexList = new ArrayList<>();
            indexList.add(CommonConstants.INDEX_PREFIX);
            ESBuilder highLight = ESBuilder.builder().build().buildHighLight("<span style=\"color:red\">", "</span>", 5, 50, highlightList);

            ESBuilder sort = ESBuilder.builder().build().buildSort("a101", "desc", false);

            SearchResponse<Object> response = esContext.searchIndex(indexList, b, from, pageSize,sort.getSorts(),highLight.getHighlight());
            // 数据量太大 log.info("查询到结果:{}", response ==null?"":JSON.toJSONString(response.hits().hits().stream().map(Hit::source).collect(Collectors.toList())));
//            return response == null ? 0 : response.hits().total().value();
            return response;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
        }
    }

    public SearchResponse<Object> searchGbTotalK(ByDb15 byDb15,Integer pageSize,Integer from,List<String> highlightList) {

        String keyNum = byDb15.getA298();

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(200);
        rspData.setMsg("查询成功");
        rspData.setRows(Collections.emptyList());
        try {
            ESBuilder b = ESBuilder.builder().build();

            if(StringUtils.isNotEmpty(keyNum) ){
                ESBuilder build = ESBuilder.builder().build();
//                build.buildQueryMutiMathDb15(CommonConstants.searchFieldA100, keyNum);
//                build.buildQueryMutiMath(CommonConstants.searchFieldA100A298, keyNum);
                build.buildQueryMutiMathDb15All(CommonConstants.searchFieldA100A298, keyNum);

                b.buildBoolMust(build.getQuery());
            }

            ESBuilder build = ESBuilder.builder().build();
            build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.gbString);
            b.buildBoolMust(build.getQuery());

            List<String> indexList = new ArrayList<>();
            indexList.add(CommonConstants.INDEX_PREFIX);
            ESBuilder highLight = ESBuilder.builder().build().buildHighLight("<span style=\"color:red\">", "</span>", 5, 50, highlightList);

            ESBuilder sort = ESBuilder.builder().build().buildSort("a101", "desc", false);

            SearchResponse<Object> response = esContext.searchIndex(indexList, b, from, pageSize,sort.getSorts(),highLight.getHighlight());
            // 数据量太大 log.info("查询到结果:{}", response ==null?"":JSON.toJSONString(response.hits().hits().stream().map(Hit::source).collect(Collectors.toList())));
//        return response == null ? 0 : response.hits().total().value();
            return response;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
        }
    }

    public SearchResponse<Object> searchHbTotalK(ByDb15 byDb15,Integer pageSize,Integer from,List<String> highlightList) {
        String keyNum = byDb15.getA298();
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(200);
        rspData.setMsg("查询成功");
        rspData.setRows(Collections.emptyList());
        try {
            ESBuilder b = ESBuilder.builder().build();

            if(StringUtils.isNotEmpty(keyNum) ){
                ESBuilder build = ESBuilder.builder().build();
//                build.buildQueryMutiMathDb15(CommonConstants.searchFieldA100, keyNum);
//                build.buildQueryMutiMath(CommonConstants.searchFieldA100A298, keyNum);
                build.buildQueryMutiMathDb15All(CommonConstants.searchFieldA100A298, keyNum);
                b.buildBoolMust(build.getQuery());
            }

            ESBuilder build = ESBuilder.builder().build();
            build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.hbString);
            b.buildBoolMust(build.getQuery());

            List<String> indexList = new ArrayList<>();
            indexList.add(CommonConstants.INDEX_PREFIX);
            ESBuilder highLight = ESBuilder.builder().build().buildHighLight("<span style=\"color:red\">", "</span>", 5, 50, highlightList);

            ESBuilder sort = ESBuilder.builder().build().buildSort("a101", "desc", false);

            SearchResponse<Object> response = esContext.searchIndex(indexList, b, from, pageSize,sort.getSorts(),highLight.getHighlight());
            // 数据量太大 log.info("查询到结果:{}", response ==null?"":JSON.toJSONString(response.hits().hits().stream().map(Hit::source).collect(Collectors.toList())));
//        return response == null ? 0 : response.hits().total().value();
            return response;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
        }
    }

    public SearchResponse<Object> searchDbTotalK(ByDb15 byDb15,Integer pageSize,Integer from,List<String> highlightList) {
        String keyNum = byDb15.getA298();
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(200);
        rspData.setMsg("查询成功");
        rspData.setRows(Collections.emptyList());
        try {
            ESBuilder b = ESBuilder.builder().build();

            if(StringUtils.isNotEmpty(keyNum) ){
                ESBuilder build = ESBuilder.builder().build();
//                build.buildQueryMutiMathDb15(CommonConstants.searchFieldA100, keyNum);
//                build.buildQueryMutiMath(CommonConstants.searchFieldA100A298, keyNum);
                build.buildQueryMutiMathDb15All(CommonConstants.searchFieldA100A298, keyNum);
                b.buildBoolMust(build.getQuery());
            }

            ESBuilder build = ESBuilder.builder().build();
            build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.dbString);
            b.buildBoolMust(build.getQuery());

            List<String> indexList = new ArrayList<>();
            indexList.add(CommonConstants.INDEX_PREFIX);
            ESBuilder highLight = ESBuilder.builder().build().buildHighLight("<span style=\"color:red\">", "</span>", 5, 50, highlightList);

            ESBuilder sort = ESBuilder.builder().build().buildSort("a101", "desc", false);

            SearchResponse<Object> response = esContext.searchIndex(indexList, b, from, pageSize,sort.getSorts(),highLight.getHighlight());
            // 数据量太大 log.info("查询到结果:{}", response ==null?"":JSON.toJSONString(response.hits().hits().stream().map(Hit::source).collect(Collectors.toList())));
//        return response == null ? 0 : response.hits().total().value();
            return response;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
        }
    }

    public SearchResponse<Object> searchTbTotalK(ByDb15 byDb15,Integer pageSize,Integer from,List<String> highlightList) {
        String keyNum = byDb15.getA298();
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(200);
        rspData.setMsg("查询成功");
        rspData.setRows(Collections.emptyList());
        try {
            ESBuilder b = ESBuilder.builder().build();

            if(StringUtils.isNotEmpty(keyNum) ){
                ESBuilder build = ESBuilder.builder().build();
//                build.buildQueryMutiMathDb15(CommonConstants.searchFieldA100, keyNum);
//                build.buildQueryMutiMath(CommonConstants.searchFieldA100A298, keyNum);
                build.buildQueryMutiMathDb15All(CommonConstants.searchFieldA100A298, keyNum);
                b.buildBoolMust(build.getQuery());
            }

            ESBuilder build = ESBuilder.builder().build();
            build.buildQueryMutiMath850bak(CommonConstants.searchFieldA850, CommonConstants.tbString);
            b.buildBoolMust(build.getQuery());

            List<String> indexList = new ArrayList<>();
            indexList.add(CommonConstants.INDEX_PREFIX);
            ESBuilder highLight = ESBuilder.builder().build().buildHighLight("<span style=\"color:red\">", "</span>", 5, 50, highlightList);

            ESBuilder sort = ESBuilder.builder().build().buildSort("a101", "desc", false);

            SearchResponse<Object> response = esContext.searchIndex(indexList, b, from, pageSize,sort.getSorts(),highLight.getHighlight());
            // 数据量太大 log.info("查询到结果:{}", response ==null?"":JSON.toJSONString(response.hits().hits().stream().map(Hit::source).collect(Collectors.toList())));
//        return response == null ? 0 : response.hits().total().value();
            return response;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
        }
    }

    public SearchResponse<Object> searchGwTotalK(ByDb15 byDb15,Integer pageSize,Integer from,List<String> highlightList) {
        String keyNum = byDb15.getA298();
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(200);
        rspData.setMsg("查询成功");
        rspData.setRows(Collections.emptyList());
        try {
            ESBuilder b = ESBuilder.builder().build();

            if(StringUtils.isNotEmpty(keyNum) ){
                ESBuilder build = ESBuilder.builder().build();
//                build.buildQueryMutiMathDb15(CommonConstants.searchFieldA100, keyNum);
//                build.buildQueryMutiMath(CommonConstants.searchFieldA100A298, keyNum);
                build.buildQueryMutiMathDb15All(CommonConstants.searchFieldA100A298, keyNum);
                b.buildBoolMust(build.getQuery());
            }

            ESBuilder build = ESBuilder.builder().build();
            build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.gwString);
            b.buildBoolMust(build.getQuery());

            List<String> indexList = new ArrayList<>();
            indexList.add(CommonConstants.INDEX_PREFIX);
            ESBuilder highLight = ESBuilder.builder().build().buildHighLight("<span style=\"color:red\">", "</span>", 5, 50, highlightList);

            ESBuilder sort = ESBuilder.builder().build().buildSort("a101", "desc", false);

            SearchResponse<Object> response = esContext.searchIndex(indexList, b, from, pageSize,sort.getSorts(),highLight.getHighlight());
            // 数据量太大 log.info("查询到结果:{}", response ==null?"":JSON.toJSONString(response.hits().hits().stream().map(Hit::source).collect(Collectors.toList())));
//        return response == null ? 0 : response.hits().total().value();
            return response;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
        }
    }




    public  String addSlashTIfGbWithNumber(String input) {
        // 校验输入不为null
        Objects.requireNonNull(input, "待处理字符串不能为null");

        // 去除前后空格，避免空格干扰判断（如"  GB 14976  " → "GB 14976"）
        String trimmedInput = input.trim();

        // 1. 正则规则：^(?i)gb → 以GB开头（不区分大小写）；
        //    (?:\\s+|) → GB后可接空格（1个及以上）或无空格；
        //    \\d+ → 紧跟数字（1个及以上）；
        //    .* → 数字后可接其他内容（如GB 14976-2020）
        String gbWithNumberPattern = "^(?i)gb(?:\\s+|)\\d+.*";
        // 2. 判断是否已包含/T（不区分大小写，避免重复添加）
        boolean hasSlashT = trimmedInput.toLowerCase().contains("gb/t");

        // 满足：GB开头+未含/T+后跟数字 → 在GB后添加/T
        if (trimmedInput.matches(gbWithNumberPattern) && !hasSlashT) {
            // 替换开头的GB（不区分大小写）为GB/T，保留后续格式（空格、数字、后缀等）
            return trimmedInput.replaceFirst("^(?i)gb", "GB/T");
        }

        // 不满足条件，返回原始字符串（含前后空格的原格式）
        return input;
    }


//    @Override
//    public TableDataInfo mutilSearchFront(ByDb15 byDb15, Integer pageNum, Integer pageSize) {
//        String a101 =  byDb15.getA101();
//        if(StringUtils.isNotEmpty(a101) && a101.length() > 5){
//            String yearStr = a101.substring(0, 4);
//            // 转换为整数并加1
//            int year = Integer.parseInt(yearStr);
//            int nextYear = year + 1;
//            byDb15.setA101(nextYear+"");
//        }
//        TableDataInfo rspData = new TableDataInfo();
//        rspData.setCode(200);
//        rspData.setMsg("查询成功");
//        rspData.setRows(Collections.emptyList());
//        try {
//            List<Map<String, Object>> dataList = new ArrayList<>();
//            List<String> highlightList = new ArrayList<>();
//            highlightList.add("a850");
//            ESBuilder b = ESBuilder.builder().build();
//            if (StringUtils.isNotNull(byDb15.getA100()) && StringUtils.isNotEmpty(byDb15.getA100())) {
//                ESBuilder build = ESBuilder.builder().build();
//                build.buildQueryMutiMathGs(CommonConstants.searchFieldA100, byDb15.getA100());
//                b.buildBoolMust(build.getQuery());
//            }
//            if (StringUtils.isNotNull(byDb15.getA298()) && StringUtils.isNotEmpty(byDb15.getA298())) {
//                ESBuilder build = ESBuilder.builder().build();
//                build.buildQueryMutiMath(CommonConstants.searchFieldA298, byDb15.getA298());
//                b.buildBoolMust(build.getQuery());
//                highlightList.add("a298");
//            }
//            if (StringUtils.isNotNull(byDb15.getA302()) && StringUtils.isNotEmpty(byDb15.getA302())) {
//                ESBuilder build = ESBuilder.builder().build();
//                build.buildQueryMutiMath(CommonConstants.searchFieldA302, byDb15.getA302());
//                b.buildBoolMust(build.getQuery());
//                highlightList.add("a302");
//            }
//            if (StringUtils.isNotNull(byDb15.getA895()) && StringUtils.isNotEmpty(byDb15.getA895())) {
//                ESBuilder build = ESBuilder.builder().build();
//                build.buildQueryMutiMath(CommonConstants.searchFieldA895, byDb15.getA895());
//                b.buildBoolMust(build.getQuery());
//                highlightList.add("a895");
//            }
//            if (StringUtils.isNotNull(byDb15.getA209()) && StringUtils.isNotEmpty(byDb15.getA209())) {
//                ESBuilder build = ESBuilder.builder().build();
//                build.buildQueryMutiMath(CommonConstants.searchFieldA209, byDb15.getA209());
//                b.buildBoolMust(build.getQuery());
//                highlightList.add("a209");
//            }
//            if (StringUtils.isNotNull(byDb15.getA825()) && StringUtils.isNotEmpty(byDb15.getA825())) {
//                ESBuilder build = ESBuilder.builder().build();
//                build.buildQueryMutiMath(CommonConstants.searchFieldA825, byDb15.getA825());
//                b.buildBoolMust(build.getQuery());
//            }
//            if (StringUtils.isNotNull(byDb15.getA826()) && StringUtils.isNotEmpty(byDb15.getA826())) {
//                ESBuilder build = ESBuilder.builder().build();
//                build.buildQueryMutiMath(CommonConstants.searchFieldA826, byDb15.getA826());
//                b.buildBoolMust(build.getQuery());
//            }
//            if (StringUtils.isNotNull(byDb15.getA200()) && StringUtils.isNotEmpty(byDb15.getA200())) {
//                ESBuilder build = ESBuilder.builder().build();
//                build.buildQueryMutiMath(CommonConstants.searchFieldA200, byDb15.getA200());
//                b.buildBoolMust(build.getQuery());
//            }
//            if (StringUtils.isNotNull(byDb15.getA850()) && StringUtils.isNotEmpty(byDb15.getA850())) {
//                ESBuilder build = ESBuilder.builder().build();
//                build.buildQueryMutiMath(CommonConstants.searchFieldA850, byDb15.getA850());
//                b.buildBoolMust(build.getQuery());
//            }
//            if (StringUtils.isNotNull(byDb15.getCategory()) && StringUtils.isNotEmpty(byDb15.getCategory())) {
//                String category = byDb15.getCategory();
//                if(category.equals("all")){
//                    ESBuilder build = ESBuilder.builder().build();
//                    build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.totalString);
//                    b.buildBoolMust(build.getQuery());
//                }else if(category.equals("gb")){
//                    ESBuilder build = ESBuilder.builder().build();
//                    build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.gbString);
//                    b.buildBoolMust(build.getQuery());
//                }else if(category.equals("hb")){
//                    ESBuilder build = ESBuilder.builder().build();
//                    build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.hbString);
//                    b.buildBoolMust(build.getQuery());
//                }else if(category.equals("db")){
//                    ESBuilder build = ESBuilder.builder().build();
//                    build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.dbString);
//                    b.buildBoolMust(build.getQuery());
//                }else if(category.equals("tb")){
//                    ESBuilder build = ESBuilder.builder().build();
//                    build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.tbString);
//                    b.buildBoolMust(build.getQuery());
//                }else if(category.equals("gw")){
//                    ESBuilder build = ESBuilder.builder().build();
//                    build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.gwString);
//                    b.buildBoolMust(build.getQuery());
//                }
//            }
//            if (StringUtils.isNotNull(byDb15.getA101()) && StringUtils.isNotEmpty(byDb15.getA101())) {
//                String year = byDb15.getA101();
//                String startDate = year + "-01-01";
//                String endDate = year + "-12-31";
//                b.buildQueryListRange(CommonConstants.searchFieldA101, "gte", startDate);
//                b.buildQueryListRange(CommonConstants.searchFieldA101, "lte", endDate);
//            }
//
//            Integer from = (pageNum - 1) * pageSize;
//            List<String> indexList = new ArrayList<>();
//            indexList.add(CommonConstants.INDEX_PREFIX);
//            ESBuilder highLight = ESBuilder.builder().build().buildHighLight("<span style=\"color:red\">", "</span>", 5, 50, highlightList);
//            ESBuilder sort = ESBuilder.builder().build().buildSort("a101", "desc", false);
//
//            SearchResponse<Object> response = esContext.searchIndex(indexList, b, from, pageSize, sort.getSorts(), highLight.getHighlight());
//            // 数据量太大 log.info("查询到结果:{}", response ==null?"":JSON.toJSONString(response.hits().hits().stream().map(Hit::source).collect(Collectors.toList())));
//            if (response != null) {
//                for (Hit<Object> hit : response.hits().hits()) {
//                    Map<String, Object> map = new HashMap<>();
//                    Map<String, Object> source = (Map<String, Object>) hit.source();
//                    Map<String, List<String>> highlight = hit.highlight();
//                    List<String> a100List = highlight.get("a100");
//                    List<String> a298List = highlight.get("a298");
//                    List<String> a302List = highlight.get("a302");
//                    List<String> a895List = highlight.get("a895");
//                    List<String> a209List = highlight.get("a209");
//
//                    map.put("a100", highlight.get("a100"));
//                    map.put("a298", highlight.get("a298"));
//                    map.put("a302", highlight.get("a302"));
//                    map.put("a895", highlight.get("a895"));
//                    map.put("a209", highlight.get("a209"));
//
//                    if (a100List != null) {
//                        map.put("a100", a100List.get(0));
//                    }else{
//                        map.put("a100", source.get("a100"));
//                    }
//                    if (a298List != null) {
//                        map.put("a298", a298List.get(0));
//                    }else{
//                        map.put("a298", source.get("a298"));
//                    }
//                    if (a302List != null) {
//                        map.put("a302", a302List.get(0));
//                    }else{
//                        map.put("a302", source.get("a302"));
//                    }
//                    if (a895List != null) {
//                        map.put("a895", a895List.get(0));
//                    }else{
//                        map.put("a895", source.get("a895"));
//                    }
//                    if (a209List != null) {
//                        map.put("a209", a209List.get(0));
//                    }else{
//                        map.put("a209", source.get("a209"));
//                    }
//
//                    String index = hit.index();
//                    map.put("index", index);
//                    map.put("id", hit.id());
//
//                    map.put("a101", source.get("a101"));
//                    map.put("a102", source.get("a102"));
//                    map.put("a200", source.get("a200"));
//                    map.put("a205", source.get("a205"));
//                    map.put("a206", source.get("a206"));
//                    map.put("a895", source.get("a895"));
//                    map.put("a330", source.get("a330"));
//                    map.put("a331", source.get("a331"));
//                    map.put("a461", source.get("a461"));
//                    map.put("a462", source.get("a462"));
//                    map.put("a825", source.get("a825"));
//                    map.put("a826", source.get("a826"));
//                    map.put("a310", source.get("a310"));
//                    map.put("filePath", source.get("filePath"));
//                    map.put("table_name", source.get("table_name"));
//                    dataList.add(map);
//                }
//            }
//
//            String category = byDb15.getCategory();
//            if(category.equals("all")){
////                long zhTotal = searchAllTotal(byDb15,pageSize);
//                long gbtotal = searchGbTotal(byDb15,pageSize);
//                long hbtotal = searchHbTotal(byDb15,pageSize);
//                long dbtotal = searchDbTotal(byDb15,pageSize);
//                long tbtotal = searchTbTotal(byDb15,pageSize);
//                long gwtotal = searchGwTotal(byDb15,pageSize);
//                rspData.setGbTotal(gbtotal);
//                rspData.setDbTotal(dbtotal);
//                rspData.setHbTotal(hbtotal);
//                rspData.setTbTotal(tbtotal);
//                rspData.setGwTotal(gwtotal);
////                rspData.setZhTotal(zhTotal);
//            }
//
//            //拼凑
//
//
//            rspData.setRows(dataList);
//            rspData.setTotal(response == null ? 0 : response.hits().total().value());
//            return rspData;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return rspData;
//        } finally {
////            try {
////                userService.saveUserBehaviorOfSearch(keyword);
////            } catch (Exception e) {
////            }
//        }
//    }


    @Override
    public TableDataInfo dbSearch(ByDb15 byDb15, Integer pageNum, Integer pageSize) {
        String a101 =  byDb15.getA101();
        if(StringUtils.isNotEmpty(a101)){
            String yearStr = a101.substring(0, 4);
            // 转换为整数并加1
            int year = Integer.parseInt(yearStr);
            int nextYear = year + 1;
            byDb15.setA101(nextYear+"");
        }
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(200);
        rspData.setMsg("查询成功");
        rspData.setRows(Collections.emptyList());
        try {
            List<Map<String, Object>> dataList = new ArrayList<>();
            List<String> highlightList = new ArrayList<>();
            highlightList.add("a850");
            ESBuilder b = ESBuilder.builder().build();

            b.buildQueryMutiMath(CommonConstants.searchFieldA850, "DB15");
//            b.buildQueryMutiMath(CommonConstants.searchFieldA100, "db15");
            b.buildBoolMust(b.getQuery());
            if (StringUtils.isNotNull(byDb15.getA100()) && StringUtils.isNotEmpty(byDb15.getA100())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMathDb15(CommonConstants.searchFieldA100, byDb15.getA100());
                b.buildBoolMust(build.getQuery());
//                highlightList.add("a100");
            }
            if (StringUtils.isNotNull(byDb15.getA298()) && StringUtils.isNotEmpty(byDb15.getA298())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA298, byDb15.getA298());
                b.buildBoolMust(build.getQuery());
                highlightList.add("a298");
            }
            if (StringUtils.isNotNull(byDb15.getA302()) && StringUtils.isNotEmpty(byDb15.getA302())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA302, byDb15.getA302());
                b.buildBoolMust(build.getQuery());
                highlightList.add("a302");
            }
            if (StringUtils.isNotNull(byDb15.getA895()) && StringUtils.isNotEmpty(byDb15.getA895())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA895, byDb15.getA895());
                b.buildBoolMust(build.getQuery());
                highlightList.add("a895");
            }
            if (StringUtils.isNotNull(byDb15.getA209()) && StringUtils.isNotEmpty(byDb15.getA209())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA209, byDb15.getA209());
                b.buildBoolMust(build.getQuery());
                highlightList.add("a209");
            }
            if (StringUtils.isNotNull(byDb15.getA825()) && StringUtils.isNotEmpty(byDb15.getA825())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA825, byDb15.getA825());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA826()) && StringUtils.isNotEmpty(byDb15.getA826())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA826, byDb15.getA826());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA200()) && StringUtils.isNotEmpty(byDb15.getA200())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA200, byDb15.getA200());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA101()) && StringUtils.isNotEmpty(byDb15.getA101())) {
//                ESBuilder build = ESBuilder.builder().build();
                // 构建范围查询（添加到 filterQuerys 列表）
                String year = byDb15.getA101();
                String startDate = year + "-01-01";
                String endDate = year + "-12-31";
                b.buildQueryListRange(CommonConstants.searchFieldA101, "gt", startDate);
                b.buildQueryListRange(CommonConstants.searchFieldA101, "lt", endDate);
                // 从 filterQuerys 中获取查询条件（注意：filterQuerys 可能为 null 或空
            }

//            if (StringUtils.isNotEmpty(syscode)) {
//                b.buildBoolMustWildCard("class_system", syscode);
//            }

            Integer from = (pageNum - 1) * pageSize;
//            List<String> indexList = getIndexByDatabaseCode(code, replace);
//            if (indexList.isEmpty()) {
//                return rspData;
//            }
            List<String> indexList = new ArrayList<>();
            indexList.add(CommonConstants.INDEX_PREFIX);
            ESBuilder highLight = ESBuilder.builder().build().buildHighLight("<span style=\"color:red\">", "</span>", 5, 50, highlightList);
//            ESBuilder sort = ESBuilder.builder().build().buildSort("create_time", "desc", false);
            ESBuilder sort = ESBuilder.builder().build().buildSort("a101", "desc", false);

            SearchResponse<Object> response = esContext.searchIndex(indexList, b, from, pageSize, sort.getSorts(), highLight.getHighlight());
            // 数据量太大 log.info("查询到结果:{}", response ==null?"":JSON.toJSONString(response.hits().hits().stream().map(Hit::source).collect(Collectors.toList())));
            if (response != null) {
                for (Hit<Object> hit : response.hits().hits()) {
                    Map<String, Object> map = new HashMap<>();
                    Map<String, Object> source = (Map<String, Object>) hit.source();
                    Map<String, List<String>> highlight = hit.highlight();
                    List<String> a100List = highlight.get("a100");
                    List<String> a298List = highlight.get("a298");
                    List<String> a302List = highlight.get("a302");
                    List<String> a895List = highlight.get("a895");
                    List<String> a209List = highlight.get("a209");

                    map.put("a100", highlight.get("a100"));
                    map.put("a298", highlight.get("a298"));
                    map.put("a302", highlight.get("a302"));
                    map.put("a895", highlight.get("a895"));
                    map.put("a209", highlight.get("a209"));

//                    List<String> abstractList = highlight.get("abstract");
//                    List<String> keywordList = highlight.get("keyword");
//                    map.put("title", source.get("title"));
//                    map.put("abstract", source.get("abstract"));
//                    map.put("keyword", source.get("keyword"));
//                    if (StringUtils.isNotEmpty(syscode)) {

                    if (a100List != null) {
                        map.put("a100", a100List.get(0));
                    }else{
                        map.put("a100", source.get("a100"));
                    }
                    if (a298List != null) {
                        map.put("a298", a298List.get(0));
                    }else{
                        map.put("a298", source.get("a298"));
                    }
                    if (a302List != null) {
                        map.put("a302", a302List.get(0));
                    }else{
                        map.put("a302", source.get("a302"));
                    }
                    if (a895List != null) {
                        map.put("a895", a895List.get(0));
                    }else{
                        map.put("a895", source.get("a895"));
                    }
                    if (a209List != null) {
                        map.put("a209", a209List.get(0));
                    }else{
                        map.put("a209", source.get("a209"));
                    }

//                        if (abstractList != null) {
//                            map.put("abstract", String.join(",", abstractList));
//                        }
//                        if (keywordList != null) {
//                            map.put("keyword", String.join(",", keywordList));
//                        }
//                    }
//                    map.put("createTime", source.get("create_time"));
                    String index = hit.index();
                    map.put("index", index);
                    map.put("id", hit.id());


                    map.put("a101", source.get("a101"));
                    map.put("a102", source.get("a102"));
                    map.put("a200", source.get("a200"));
                    map.put("a205", source.get("a205"));
                    map.put("a206", source.get("a206"));
//                    map.put("a895", source.get("a895"));
                    map.put("a330", source.get("a330"));
                    map.put("a331", source.get("a331"));
                    map.put("a461", source.get("a461"));
                    map.put("a462", source.get("a462"));
                    map.put("a310", source.get("a310"));
                    map.put("a825", source.get("a825"));
                    map.put("a826", source.get("a826"));
                    map.put("table_name", source.get("table_name"));
                    map.put("filePath", source.get("filePath"));

//                    List<Map<String, String>> fileList = dataContext.excuteQueryList("select filelinkurl,filesize from " + index.replace(CommonConstants.INDEX_PREFIX, "") + "_metadata_file where recordid = " + hit.id());
//                    if (!fileList.isEmpty()) {
//                        map.put("fileUrl", fileList.get(0).get("filelinkurl"));
//                        map.put("fileSize", fileList.get(0).get("filesize") == null ? 0 : new BigDecimal(fileList.get(0).get("filesize")).divide(new BigDecimal(1024), 0, RoundingMode.HALF_UP));
//                        map.put("fileSizeStr", fileList.get(0).get("filesize") == null ? "0B" : FileUtil.formatFileSize(Long.parseLong(fileList.get(0).get("filesize"))));
//                        if (fileList.get(0).get("filelinkurl").lastIndexOf(".") > 0) {
//                            String docType = fileList.get(0).get("filelinkurl").substring(fileList.get(0).get("filelinkurl").lastIndexOf(".") + 1);
//                            String[] allow = allowDocType.split(",");
//                            map.put("canPreview", Arrays.asList(allow).contains(docType));
//                        }
//                    }
                    dataList.add(map);
                }
            }
            rspData.setRows(dataList);
            rspData.setTotal(response == null ? 0 : response.hits().total().value());
            return rspData;
        } catch (Exception e) {
            e.printStackTrace();
            return rspData;
        } finally {
//            try {
//                userService.saveUserBehaviorOfSearch(keyword);
//            } catch (Exception e) {
//            }
        }
    }

    @Override
    public TableDataInfo gbSearch(ByDb15 byDb15, Integer pageNum, Integer pageSize) {
        String a101 =  byDb15.getA101();
        if(StringUtils.isNotEmpty(a101)){
            String yearStr = a101.substring(0, 4);
            // 转换为整数并加1
            int year = Integer.parseInt(yearStr);
            int nextYear = year + 1;
            byDb15.setA101(nextYear+"");
        }
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(200);
        rspData.setMsg("查询成功");
        rspData.setRows(Collections.emptyList());
        try {
            List<Map<String, Object>> dataList = new ArrayList<>();
            List<String> highlightList = new ArrayList<>();
            highlightList.add("a850");
            ESBuilder b = ESBuilder.builder().build();


            b.buildQueryMutiMath(CommonConstants.searchFieldA850, "GB");
//            b.buildQueryMutiMath(CommonConstants.searchFieldA100, "db15");
            b.buildBoolMust(b.getQuery());

            if (StringUtils.isNotNull(byDb15.getA100()) && StringUtils.isNotEmpty(byDb15.getA100())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMathGb(CommonConstants.searchFieldA100, byDb15.getA100());
                b.buildBoolMust(build.getQuery());
                highlightList.add("a100");
            }
            if (StringUtils.isNotNull(byDb15.getA298()) && StringUtils.isNotEmpty(byDb15.getA298())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA298, byDb15.getA298());
                b.buildBoolMust(build.getQuery());
                highlightList.add("a298");
            }
            if (StringUtils.isNotNull(byDb15.getA302()) && StringUtils.isNotEmpty(byDb15.getA302())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA302, byDb15.getA302());
                b.buildBoolMust(build.getQuery());
                highlightList.add("a302");
            }
            if (StringUtils.isNotNull(byDb15.getA895()) && StringUtils.isNotEmpty(byDb15.getA895())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA895, byDb15.getA895());
                b.buildBoolMust(build.getQuery());
                highlightList.add("a895");
            }
            if (StringUtils.isNotNull(byDb15.getA209()) && StringUtils.isNotEmpty(byDb15.getA209())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA209, byDb15.getA209());
                b.buildBoolMust(build.getQuery());
                highlightList.add("a209");
            }
            if (StringUtils.isNotNull(byDb15.getA825()) && StringUtils.isNotEmpty(byDb15.getA825())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA825, byDb15.getA825());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA826()) && StringUtils.isNotEmpty(byDb15.getA826())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA826, byDb15.getA826());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA200()) && StringUtils.isNotEmpty(byDb15.getA200())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA200, byDb15.getA200());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA101()) && StringUtils.isNotEmpty(byDb15.getA101())) {
//                ESBuilder build = ESBuilder.builder().build();
                // 构建范围查询（添加到 filterQuerys 列表）
                String year = byDb15.getA101();
                String startDate = year + "-01-01";
                String endDate = year + "-12-31";
                b.buildQueryListRange(CommonConstants.searchFieldA101, "gt", startDate);
                b.buildQueryListRange(CommonConstants.searchFieldA101, "lt", endDate);
                // 从 filterQuerys 中获取查询条件（注意：filterQuerys 可能为 null 或空
            }

//            if (StringUtils.isNotEmpty(syscode)) {
//                b.buildBoolMustWildCard("class_system", syscode);
//            }

            Integer from = (pageNum - 1) * pageSize;
//            List<String> indexList = getIndexByDatabaseCode(code, replace);
//            if (indexList.isEmpty()) {
//                return rspData;
//            }
            List<String> indexList = new ArrayList<>();
            indexList.add(CommonConstants.NEW_INDEX_PREFIX);
            ESBuilder highLight = ESBuilder.builder().build().buildHighLight("<span style=\"color:red\">", "</span>", 5, 50, highlightList);
//            ESBuilder sort = ESBuilder.builder().build().buildSort("create_time", "desc", false);
            ESBuilder sort = ESBuilder.builder().build().buildSort("a101", "desc", false);
            SearchResponse<Object> response = esContext.searchIndex(indexList, b, from, pageSize, sort.getSorts(), highLight.getHighlight());
            // 数据量太大 log.info("查询到结果:{}", response ==null?"":JSON.toJSONString(response.hits().hits().stream().map(Hit::source).collect(Collectors.toList())));
            if (response != null) {
                for (Hit<Object> hit : response.hits().hits()) {
                    Map<String, Object> map = new HashMap<>();
                    Map<String, Object> source = (Map<String, Object>) hit.source();
                    Map<String, List<String>> highlight = hit.highlight();
                    List<String> a100List = highlight.get("a100");
                    List<String> a298List = highlight.get("a298");
                    List<String> a302List = highlight.get("a302");
                    List<String> a895List = highlight.get("a895");
                    List<String> a209List = highlight.get("a209");

                    map.put("a100", highlight.get("a100"));
                    map.put("a298", highlight.get("a298"));
                    map.put("a302", highlight.get("a302"));
                    map.put("a895", highlight.get("a895"));
                    map.put("a209", highlight.get("a209"));

//                    List<String> abstractList = highlight.get("abstract");
//                    List<String> keywordList = highlight.get("keyword");
//                    map.put("title", source.get("title"));
//                    map.put("abstract", source.get("abstract"));
//                    map.put("keyword", source.get("keyword"));
//                    if (StringUtils.isNotEmpty(syscode)) {

                    if (a100List != null) {
                        map.put("a100", a100List.get(0));
                    }else{
                        map.put("a100", source.get("a100"));
                    }
                    if (a298List != null) {
                        map.put("a298", a298List.get(0));
                    }else{
                        map.put("a298", source.get("a298"));
                    }
                    if (a302List != null) {
                        map.put("a302", a302List.get(0));
                    }else{
                        map.put("a302", source.get("a302"));
                    }
                    if (a895List != null) {
                        map.put("a895", a895List.get(0));
                    }else{
                        map.put("a895", source.get("a895"));
                    }
                    if (a209List != null) {
                        map.put("a209", a209List.get(0));
                    }else{
                        map.put("a209", source.get("a209"));
                    }

//                        if (abstractList != null) {
//                            map.put("abstract", String.join(",", abstractList));
//                        }
//                        if (keywordList != null) {
//                            map.put("keyword", String.join(",", keywordList));
//                        }
//                    }
//                    map.put("createTime", source.get("create_time"));
                    String index = hit.index();
                    map.put("index", index);
                    map.put("id", hit.id());


                    map.put("a101", source.get("a101"));
//                    if(){
//
//                    }
                    map.put("a102", source.get("a102"));
                    map.put("a200", source.get("a200"));
                    map.put("a205", source.get("a205"));
                    map.put("a206", source.get("a206"));
                    map.put("a895", source.get("a895"));
                    map.put("a330", source.get("a330"));
                    map.put("a331", source.get("a331"));
                    map.put("a461", source.get("a461"));
                    map.put("a462", source.get("a462"));
                    map.put("a825", source.get("a825"));
                    map.put("a826", source.get("a826"));
                    map.put("a310", source.get("a310"));
                    map.put("filePath", source.get("filePath"));
                    map.put("table_name", source.get("table_name"));




//                    List<Map<String, String>> fileList = dataContext.excuteQueryList("select filelinkurl,filesize from " + index.replace(CommonConstants.INDEX_PREFIX, "") + "_metadata_file where recordid = " + hit.id());
//                    if (!fileList.isEmpty()) {
//                        map.put("fileUrl", fileList.get(0).get("filelinkurl"));
//                        map.put("fileSize", fileList.get(0).get("filesize") == null ? 0 : new BigDecimal(fileList.get(0).get("filesize")).divide(new BigDecimal(1024), 0, RoundingMode.HALF_UP));
//                        map.put("fileSizeStr", fileList.get(0).get("filesize") == null ? "0B" : FileUtil.formatFileSize(Long.parseLong(fileList.get(0).get("filesize"))));
//                        if (fileList.get(0).get("filelinkurl").lastIndexOf(".") > 0) {
//                            String docType = fileList.get(0).get("filelinkurl").substring(fileList.get(0).get("filelinkurl").lastIndexOf(".") + 1);
//                            String[] allow = allowDocType.split(",");
//                            map.put("canPreview", Arrays.asList(allow).contains(docType));
//                        }
//                    }
                    dataList.add(map);
                }
            }
            rspData.setRows(dataList);
            rspData.setTotal(response == null ? 0 : response.hits().total().value());
            return rspData;
        } catch (Exception e) {
            e.printStackTrace();
            return rspData;
        } finally {
//            try {
//                userService.saveUserBehaviorOfSearch(keyword);
//            } catch (Exception e) {
//            }
        }
    }

    @Override
    public TableDataInfo mbSearch(ByDb15 byDb15, Integer pageNum, Integer pageSize) {
        String a101 =  byDb15.getA101();
        if(StringUtils.isNotEmpty(a101)){
            String yearStr = a101.substring(0, 4);
            // 转换为整数并加1
            int year = Integer.parseInt(yearStr);
            int nextYear = year + 1;
            byDb15.setA101(nextYear+"");
        }
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(200);
        rspData.setMsg("查询成功");
        rspData.setRows(Collections.emptyList());
        try {
            List<Map<String, Object>> dataList = new ArrayList<>();
            List<String> highlightList = new ArrayList<>();
            highlightList.add("a850");
            ESBuilder b = ESBuilder.builder().build();

            b.buildQueryMutiMath(CommonConstants.searchFieldA850, "mzbp");
//            b.buildQueryMutiMath(CommonConstants.searchFieldA100, "db15");
            b.buildBoolMust(b.getQuery());
            if (StringUtils.isNotNull(byDb15.getA100()) && StringUtils.isNotEmpty(byDb15.getA100())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMathMb(CommonConstants.searchFieldA100, byDb15.getA100());
                b.buildBoolMust(build.getQuery());
//                highlightList.add("a100");
            }
            if (StringUtils.isNotNull(byDb15.getA298()) && StringUtils.isNotEmpty(byDb15.getA298())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA298, byDb15.getA298());
                b.buildBoolMust(build.getQuery());
                highlightList.add("a298");
            }
            if (StringUtils.isNotNull(byDb15.getA302()) && StringUtils.isNotEmpty(byDb15.getA302())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA302, byDb15.getA302());
                b.buildBoolMust(build.getQuery());
                highlightList.add("a302");
            }
            if (StringUtils.isNotNull(byDb15.getA895()) && StringUtils.isNotEmpty(byDb15.getA895())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA895, byDb15.getA895());
                b.buildBoolMust(build.getQuery());
                highlightList.add("a895");
            }
            if (StringUtils.isNotNull(byDb15.getA209()) && StringUtils.isNotEmpty(byDb15.getA209())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA209, byDb15.getA209());
                b.buildBoolMust(build.getQuery());
                highlightList.add("a209");
            }
            if (StringUtils.isNotNull(byDb15.getA825()) && StringUtils.isNotEmpty(byDb15.getA825())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA825, byDb15.getA825());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA826()) && StringUtils.isNotEmpty(byDb15.getA826())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA826, byDb15.getA826());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA200()) && StringUtils.isNotEmpty(byDb15.getA200())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA200, byDb15.getA200());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA101()) && StringUtils.isNotEmpty(byDb15.getA101())) {
//                ESBuilder build = ESBuilder.builder().build();
                // 构建范围查询（添加到 filterQuerys 列表）
                String year = byDb15.getA101();
                String startDate = year + "-01-01";
                String endDate = year + "-12-31";
                b.buildQueryListRange(CommonConstants.searchFieldA101, "gt", startDate);
                b.buildQueryListRange(CommonConstants.searchFieldA101, "lt", endDate);
                // 从 filterQuerys 中获取查询条件（注意：filterQuerys 可能为 null 或空
            }

//            if (StringUtils.isNotEmpty(syscode)) {
//                b.buildBoolMustWildCard("class_system", syscode);
//            }

            Integer from = (pageNum - 1) * pageSize;
//            List<String> indexList = getIndexByDatabaseCode(code, replace);
//            if (indexList.isEmpty()) {
//                return rspData;
//            }
            List<String> indexList = new ArrayList<>();
            indexList.add(CommonConstants.INDEX_PREFIX);
            ESBuilder highLight = ESBuilder.builder().build().buildHighLight("<span style=\"color:red\">", "</span>", 5, 50, highlightList);
//            ESBuilder sort = ESBuilder.builder().build().buildSort("create_time", "desc", false);
            ESBuilder sort = ESBuilder.builder().build().buildSort("a101", "desc", false);

            SearchResponse<Object> response = esContext.searchIndex(indexList, b, from, pageSize, sort.getSorts(), highLight.getHighlight());
            // 数据量太大 log.info("查询到结果:{}", response ==null?"":JSON.toJSONString(response.hits().hits().stream().map(Hit::source).collect(Collectors.toList())));
            if (response != null) {
                for (Hit<Object> hit : response.hits().hits()) {
                    Map<String, Object> map = new HashMap<>();
                    Map<String, Object> source = (Map<String, Object>) hit.source();
                    Map<String, List<String>> highlight = hit.highlight();
                    List<String> a100List = highlight.get("a100");
                    List<String> a298List = highlight.get("a298");
                    List<String> a302List = highlight.get("a302");
                    List<String> a895List = highlight.get("a895");
                    List<String> a209List = highlight.get("a209");

                    map.put("a100", highlight.get("a100"));
                    map.put("a298", highlight.get("a298"));
                    map.put("a302", highlight.get("a302"));
                    map.put("a895", highlight.get("a895"));
                    map.put("a209", highlight.get("a209"));


//                    List<String> abstractList = highlight.get("abstract");
//                    List<String> keywordList = highlight.get("keyword");
//                    map.put("title", source.get("title"));
//                    map.put("abstract", source.get("abstract"));
//                    map.put("keyword", source.get("keyword"));
//                    if (StringUtils.isNotEmpty(syscode)) {

                    if (a100List != null) {
                        map.put("a100", a100List.get(0));
                    }else{
                        map.put("a100", source.get("a100"));
                    }
                    if (a298List != null) {
                        map.put("a298", a298List.get(0));
                    }else{
                        map.put("a298", source.get("a298"));
                    }
                    if (a302List != null) {
                        map.put("a302", a302List.get(0));
                    }else{
                        map.put("a302", source.get("a302"));
                    }
                    if (a895List != null) {
                        map.put("a895", a895List.get(0));
                    }else{
                        map.put("a895", source.get("a895"));
                    }
                    if (a209List != null) {
                        map.put("a209", a209List.get(0));
                    }else{
                        map.put("a209", source.get("a209"));
                    }

//                        if (abstractList != null) {
//                            map.put("abstract", String.join(",", abstractList));
//                        }
//                        if (keywordList != null) {
//                            map.put("keyword", String.join(",", keywordList));
//                        }
//                    }
//                    map.put("createTime", source.get("create_time"));
                    String index = hit.index();
                    map.put("index", index);
                    map.put("id", hit.id());


                    map.put("a101", source.get("a101"));
//                    if(){
//
//                    }
                    map.put("a102", source.get("a102"));
                    map.put("a200", source.get("a200"));
                    map.put("a205", source.get("a205"));
                    map.put("a206", source.get("a206"));
                    map.put("a895", source.get("a895"));
                    map.put("a330", source.get("a330"));
                    map.put("a331", source.get("a331"));
                    map.put("a461", source.get("a461"));
                    map.put("a462", source.get("a462"));
                    map.put("a825", source.get("a825"));
                    map.put("a826", source.get("a826"));
                    map.put("a310", source.get("a310"));
                    map.put("table_name", source.get("table_name"));
                    map.put("filePath", source.get("filePath"));



//                    List<Map<String, String>> fileList = dataContext.excuteQueryList("select filelinkurl,filesize from " + index.replace(CommonConstants.INDEX_PREFIX, "") + "_metadata_file where recordid = " + hit.id());
//                    if (!fileList.isEmpty()) {
//                        map.put("fileUrl", fileList.get(0).get("filelinkurl"));
//                        map.put("fileSize", fileList.get(0).get("filesize") == null ? 0 : new BigDecimal(fileList.get(0).get("filesize")).divide(new BigDecimal(1024), 0, RoundingMode.HALF_UP));
//                        map.put("fileSizeStr", fileList.get(0).get("filesize") == null ? "0B" : FileUtil.formatFileSize(Long.parseLong(fileList.get(0).get("filesize"))));
//                        if (fileList.get(0).get("filelinkurl").lastIndexOf(".") > 0) {
//                            String docType = fileList.get(0).get("filelinkurl").substring(fileList.get(0).get("filelinkurl").lastIndexOf(".") + 1);
//                            String[] allow = allowDocType.split(",");
//                            map.put("canPreview", Arrays.asList(allow).contains(docType));
//                        }
//                    }
                    dataList.add(map);
                }
            }
            rspData.setRows(dataList);
            rspData.setTotal(response == null ? 0 : response.hits().total().value());
            return rspData;
        } catch (Exception e) {
            e.printStackTrace();
            return rspData;
        } finally {
//            try {
//                userService.saveUserBehaviorOfSearch(keyword);
//            } catch (Exception e) {
//            }
        }
    }


    @Override
    public TableDataInfo hbSearch(ByDb15 byDb15, Integer pageNum, Integer pageSize) {
        String a101 =  byDb15.getA101();
        if(StringUtils.isNotEmpty(a101)){
            String yearStr = a101.substring(0, 4);
            // 转换为整数并加1
            int year = Integer.parseInt(yearStr);
            int nextYear = year + 1;
            byDb15.setA101(nextYear+"");
        }
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(200);
        rspData.setMsg("查询成功");
        rspData.setRows(Collections.emptyList());
        try {
            List<Map<String, Object>> dataList = new ArrayList<>();
            List<String> highlightList = new ArrayList<>();
            highlightList.add("a850");
            ESBuilder b = ESBuilder.builder().build();

            b.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.hbString);
//            b.buildQueryMutiMath(CommonConstants.searchFieldA100, "db15");
            b.buildBoolMust(b.getQuery());

            if (StringUtils.isNotNull(byDb15.getA100()) && StringUtils.isNotEmpty(byDb15.getA100())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMathHb(CommonConstants.searchFieldA100, byDb15.getA100());
                b.buildBoolMust(build.getQuery());
//                highlightList.add("a100");
            }
            if (StringUtils.isNotNull(byDb15.getA298()) && StringUtils.isNotEmpty(byDb15.getA298())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA298, byDb15.getA298());
                b.buildBoolMust(build.getQuery());
                highlightList.add("a298");
            }
            if (StringUtils.isNotNull(byDb15.getA302()) && StringUtils.isNotEmpty(byDb15.getA302())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA302, byDb15.getA302());
                b.buildBoolMust(build.getQuery());
                highlightList.add("a302");
            }
            if (StringUtils.isNotNull(byDb15.getA895()) && StringUtils.isNotEmpty(byDb15.getA895())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA895, byDb15.getA895());
                b.buildBoolMust(build.getQuery());
                highlightList.add("a895");
            }
            if (StringUtils.isNotNull(byDb15.getA209()) && StringUtils.isNotEmpty(byDb15.getA209())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA209, byDb15.getA209());
                b.buildBoolMust(build.getQuery());
                highlightList.add("a209");
            }
            if (StringUtils.isNotNull(byDb15.getA825()) && StringUtils.isNotEmpty(byDb15.getA825())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA825, byDb15.getA825());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA826()) && StringUtils.isNotEmpty(byDb15.getA826())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA826, byDb15.getA826());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA200()) && StringUtils.isNotEmpty(byDb15.getA200())) {
                ESBuilder build = ESBuilder.builder().build();
                build.buildQueryMutiMath(CommonConstants.searchFieldA200, byDb15.getA200());
                b.buildBoolMust(build.getQuery());
            }
            if (StringUtils.isNotNull(byDb15.getA101()) && StringUtils.isNotEmpty(byDb15.getA101())) {
//                ESBuilder build = ESBuilder.builder().build();
                // 构建范围查询（添加到 filterQuerys 列表）
                String year = byDb15.getA101();
                String startDate = year + "-01-01";
                String endDate = year + "-12-31";
                b.buildQueryListRange(CommonConstants.searchFieldA101, "gt", startDate);
                b.buildQueryListRange(CommonConstants.searchFieldA101, "lt", endDate);
                // 从 filterQuerys 中获取查询条件（注意：filterQuerys 可能为 null 或空
            }

//            if (StringUtils.isNotEmpty(syscode)) {
//                b.buildBoolMustWildCard("class_system", syscode);
//            }

            Integer from = (pageNum - 1) * pageSize;
//            List<String> indexList = getIndexByDatabaseCode(code, replace);
//            if (indexList.isEmpty()) {
//                return rspData;
//            }
            List<String> indexList = new ArrayList<>();
            indexList.add(CommonConstants.INDEX_PREFIX);
            ESBuilder highLight = ESBuilder.builder().build().buildHighLight("<span style=\"color:red\">", "</span>", 5, 50, highlightList);
//            ESBuilder sort = ESBuilder.builder().build().buildSort("create_time", "desc", false);
            ESBuilder sort = ESBuilder.builder().build().buildSort("a101", "desc", false);

            SearchResponse<Object> response = esContext.searchIndex(indexList, b, from, pageSize, sort.getSorts(), highLight.getHighlight());
            // 数据量太大 log.info("查询到结果:{}", response ==null?"":JSON.toJSONString(response.hits().hits().stream().map(Hit::source).collect(Collectors.toList())));
            if (response != null) {
                for (Hit<Object> hit : response.hits().hits()) {
                    Map<String, Object> map = new HashMap<>();
                    Map<String, Object> source = (Map<String, Object>) hit.source();
                    Map<String, List<String>> highlight = hit.highlight();
                    List<String> a100List = highlight.get("a100");
                    List<String> a298List = highlight.get("a298");
                    List<String> a302List = highlight.get("a302");
                    List<String> a895List = highlight.get("a895");
                    List<String> a209List = highlight.get("a209");

                    map.put("a100", highlight.get("a100"));
                    map.put("a298", highlight.get("a298"));
                    map.put("a302", highlight.get("a302"));
                    map.put("a895", highlight.get("a895"));
                    map.put("a209", highlight.get("a209"));


//                    List<String> abstractList = highlight.get("abstract");
//                    List<String> keywordList = highlight.get("keyword");
//                    map.put("title", source.get("title"));
//                    map.put("abstract", source.get("abstract"));
//                    map.put("keyword", source.get("keyword"));
//                    if (StringUtils.isNotEmpty(syscode)) {

                    if (a100List != null) {
                        map.put("a100", a100List.get(0));
                    }else{
                        map.put("a100", source.get("a100"));
                    }
                    if (a298List != null) {
                        map.put("a298", a298List.get(0));
                    }else{
                        map.put("a298", source.get("a298"));
                    }
                    if (a302List != null) {
                        map.put("a302", a302List.get(0));
                    }else{
                        map.put("a302", source.get("a302"));
                    }
                    if (a895List != null) {
                        map.put("a895", a895List.get(0));
                    }else{
                        map.put("a895", source.get("a895"));
                    }
                    if (a209List != null) {
                        map.put("a209", a209List.get(0));
                    }else{
                        map.put("a209", source.get("a209"));
                    }

//                        if (abstractList != null) {
//                            map.put("abstract", String.join(",", abstractList));
//                        }
//                        if (keywordList != null) {
//                            map.put("keyword", String.join(",", keywordList));
//                        }
//                    }
//                    map.put("createTime", source.get("create_time"));
                    String index = hit.index();
                    map.put("index", index);
                    map.put("id", hit.id());


                    map.put("a101", source.get("a101"));
//                    if(){
//
//                    }
                    map.put("a102", source.get("a102"));
                    map.put("a200", source.get("a200"));
                    map.put("a205", source.get("a205"));
                    map.put("a206", source.get("a206"));
                    map.put("a895", source.get("a895"));
                    map.put("a330", source.get("a330"));
                    map.put("a331", source.get("a331"));
                    map.put("a461", source.get("a461"));
                    map.put("a462", source.get("a462"));
                    map.put("a825", source.get("a825"));
                    map.put("a826", source.get("a826"));
                    map.put("a310", source.get("a310"));
                    map.put("table_name", source.get("table_name"));
                    map.put("filePath", source.get("filePath"));



//                    List<Map<String, String>> fileList = dataContext.excuteQueryList("select filelinkurl,filesize from " + index.replace(CommonConstants.INDEX_PREFIX, "") + "_metadata_file where recordid = " + hit.id());
//                    if (!fileList.isEmpty()) {
//                        map.put("fileUrl", fileList.get(0).get("filelinkurl"));
//                        map.put("fileSize", fileList.get(0).get("filesize") == null ? 0 : new BigDecimal(fileList.get(0).get("filesize")).divide(new BigDecimal(1024), 0, RoundingMode.HALF_UP));
//                        map.put("fileSizeStr", fileList.get(0).get("filesize") == null ? "0B" : FileUtil.formatFileSize(Long.parseLong(fileList.get(0).get("filesize"))));
//                        if (fileList.get(0).get("filelinkurl").lastIndexOf(".") > 0) {
//                            String docType = fileList.get(0).get("filelinkurl").substring(fileList.get(0).get("filelinkurl").lastIndexOf(".") + 1);
//                            String[] allow = allowDocType.split(",");
//                            map.put("canPreview", Arrays.asList(allow).contains(docType));
//                        }
//                    }
                    dataList.add(map);
                }
            }
            rspData.setRows(dataList);
            rspData.setTotal(response == null ? 0 : response.hits().total().value());
            return rspData;
        } catch (Exception e) {
            e.printStackTrace();
            return rspData;
        } finally {
//            try {
//                userService.saveUserBehaviorOfSearch(keyword);
//            } catch (Exception e) {
//            }
        }
    }

    @Override
    public ByDb15 getDetail(String id) {
        try{
            Map<String,Object> map = esContext.searchIndexDoc(CommonConstants.INDEX_PREFIX,id);
            ByDb15 byDb15 = new ByDb15();
            byDb15.setId(map.get("id").toString());

            if(map.get("a100")!=null){
                byDb15.setA100(map.get("a100").toString());
            }
            if(map.get("a101")!=null){
                byDb15.setA101(map.get("a101").toString());
            }
            if(map.get("a102")!=null){
                byDb15.setA102(map.get("a102").toString());
            }
            if(map.get("a200")!=null){
                byDb15.setA200(map.get("a200").toString());
            }
            if(map.get("a204")!=null){
                byDb15.setA204(map.get("a204").toString());
            }
            if(map.get("a205")!=null){
                byDb15.setA205(map.get("a205").toString());
            }
            if(map.get("a206")!=null){
                byDb15.setA206(map.get("a206").toString());
            }
            if(map.get("a209")!=null){
                byDb15.setA209(map.get("a209").toString());
            }
            if(map.get("a298")!=null){
                byDb15.setA298(map.get("a298").toString());
            }
            if(map.get("a301")!=null){
                byDb15.setA301(map.get("a301").toString());
            }
            if(map.get("a302")!=null){
                byDb15.setA302(map.get("a302").toString());
            }
            if(map.get("a310")!=null){
                byDb15.setA310(map.get("a310").toString());
            }
            if(map.get("a330")!=null){
                byDb15.setA330(map.get("a330").toString());
            }
            if(map.get("a409")!=null){
                byDb15.setA409(map.get("a409").toString());
            }
            if(map.get("a461")!=null){
                byDb15.setA461(map.get("a461").toString());
            }
            if(map.get("a462")!=null){
                byDb15.setA462(map.get("a462").toString());
            }
            if(map.get("a807")!=null){
                byDb15.setA807(map.get("a807").toString());
            }
            if(map.get("a825")!=null){
                byDb15.setA825(map.get("a825").toString());
            }
            if(map.get("a826")!=null){
                byDb15.setA826(map.get("a826").toString());
            }
            if(map.get("a835")!=null){
                byDb15.setA835(map.get("a835").toString());
            }
            if(map.get("a850")!=null){
                byDb15.setA850(map.get("a850").toString());
            }
            if(map.get("a893")!=null){
                byDb15.setA893(map.get("a893").toString());
            }
            if(map.get("a894")!=null){
                byDb15.setA894(map.get("a894").toString());
            }
            if(map.get("a895")!=null){
                byDb15.setA895(map.get("a895").toString());
            }
            if(map.get("filePath")!=null){
                byDb15.setFilePath(map.get("filePath").toString());
            }
            if(map.get("table_name")!=null){
                byDb15.setTableName(map.get("table_name").toString());
            }
//            byDb15.setA461(map.get("a461").toString());
//            byDb15.setA462(map.get("a462").toString());
//            byDb15.setA409(map.get("a409").toString());
//            byDb15.setA807(map.get("a807").toString());
//            byDb15.setA209(map.get("a209").toString());
//            byDb15.setA825(map.get("a825").toString());
//            byDb15.setA826(map.get("a826").toString());
//            byDb15.setA302(map.get("a302").toString());
//            byDb15.setA204(map.get("a204").toString());
//            byDb15.setA205(map.get("a205").toString());
//            byDb15.setId(map.get("id").toString());
//            byDb15.setA206(map.get("a206").toString());
//            byDb15.setA100(map.get("a100").toString());
//            byDb15.setA101(map.get("a101").toString());
//            byDb15.setA200(map.get("a200").toString());
//            byDb15.setA102(map.get("a102").toString());
//            byDb15.setA301(map.get("a301").toString());
//            byDb15.setA330(map.get("a330").toString());
//            byDb15.setA835(map.get("a835").toString());
//            byDb15.setA298(map.get("a298").toString());
//            byDb15.setA893(map.get("a893").toString());
//            byDb15.setA850(map.get("a850").toString());
//            byDb15.setA894(map.get("a894").toString());
//            byDb15.setA895(map.get("a895").toString());
//            byDb15.setA000(map.get("a000").toString());
//            byDb15.setA001(map.get("a001").toString());
//            byDb15.setA003(map.get("a003").toString());
//            byDb15.setA010(map.get("a010").toString());
//            byDb15.setA011(map.get("a011").toString());
//            byDb15.setA103(map.get("a103").toString());
//            byDb15.setA104(map.get("a104").toString());
//            byDb15.setA203(map.get("a203").toString());
//            byDb15.setA204(map.get("a204").toString());
//            byDb15.setA205(map.get("a205").toString());
//            byDb15.setA206(map.get("a206").toString());
//            byDb15.setA207(map.get("a207").toString());
//            byDb15.setA208(map.get("a208").toString());
//            byDb15.setA210(map.get("a210").toString());
//            byDb15.setA300(map.get("a300").toString());
//            byDb15.setA301(map.get("a301").toString());
//            byDb15.setA302(map.get("a302").toString());
//            byDb15.setA304(map.get("a304").toString());
//            byDb15.setA305(map.get("a305").toString());
//            byDb15.setA309(map.get("a309").toString());
//            byDb15.setA310(map.get("a310").toString());
//            byDb15.setA311(map.get("a311").toString());
//            byDb15.setA331(map.get("a331").toString());
//            byDb15.setA390(map.get("a390").toString());
//            byDb15.setA448(map.get("a448").toString());
//            byDb15.setA502(map.get("a502").toString());
//            byDb15.setA505(map.get("a505").toString());
//            byDb15.setA800(map.get("a800").toString());
//            byDb15.setA805(map.get("a805").toString());
//            byDb15.setA808(map.get("a808").toString());
//            byDb15.setA502(map.get("a502").toString());
//            byDb15.setA505(map.get("a505").toString());
//            byDb15.setA800(map.get("a800").toString());
//            byDb15.setA805(map.get("a805").toString());
//            byDb15.setA807(map.get("a807").toString());
//            byDb15.setA808(map.get("a808").toString());
//            byDb15.setA820(map.get("a820").toString());
//            byDb15.setA821(map.get("a821").toString());
//            byDb15.setA823(map.get("a823").toString());
//            byDb15.setA824(map.get("a824").toString());
//            byDb15.setA840(map.get("a840").toString());
//            byDb15.setA837(map.get("a837").toString());
//            byDb15.setA845(map.get("a845").toString());
//            byDb15.setA846(map.get("a846").toString());
//            byDb15.setA847(map.get("a847").toString());
//            byDb15.setA860(map.get("a860").toString());
//            byDb15.setA861(map.get("a861").toString());
//            byDb15.setA863(map.get("a863").toString());
//            byDb15.setA864(map.get("a864").toString());
//            byDb15.setA865(map.get("a865").toString());
//            byDb15.setA866(map.get("a866").toString());
//            byDb15.setA870(map.get("a870").toString());
//            byDb15.setA871(map.get("a871").toString());
//            byDb15.setA885(map.get("a885").toString());
//            byDb15.setA886(map.get("a886").toString());
//            byDb15.setA890(map.get("a890").toString());
//            byDb15.setA891(map.get("a891").toString());
//            byDb15.setA892(map.get("a892").toString());
//            byDb15.setA893(map.get("a893").toString());
//            byDb15.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(map.get("create_time").toString()));
//            byDb15.setTextAddress(map.get("textAddress").toString());
//            byDb15.setFilePath(map.get("filePath").toString());
//            byDb15.setReserveA(map.get("reserve_a").toString());
//            byDb15.setReserveB(map.get("reserve_b").toString());
//            byDb15.setReserveC(map.get("reserve_c").toString());
//            byDb15.setReserveD(map.get("reserve_d").toString());
//            byDb15.setReserveE(map.get("reserve_e").toString());
//            byDb15.setTableName(map.get("table_name").toString());
            return byDb15;
        }catch (Exception e){
            return null;
        }
    }

    @Override
    public AjaxResult edit(ByDb15 byDb15) {
        Map<String, Object> doc = new HashMap<>();
        doc.put("id", byDb15.getId());
        doc.put("a100", byDb15.getA100());
        doc.put("a101", byDb15.getA101());
        doc.put("a102", byDb15.getA102());
        doc.put("a200", byDb15.getA200());
        doc.put("a204", byDb15.getA204());
        doc.put("a205", byDb15.getA205());
        doc.put("a206", byDb15.getA206());
        doc.put("a209", byDb15.getA209());
        doc.put("a298", byDb15.getA298());
        doc.put("a301", byDb15.getA301());
        doc.put("a302", byDb15.getA302());
        doc.put("a330", byDb15.getA330());
        doc.put("a310", byDb15.getA310());
        doc.put("a409", byDb15.getA409());
        doc.put("a461", byDb15.getA461());
        doc.put("a462", byDb15.getA462());
        doc.put("a807", byDb15.getA807());
        doc.put("a825", byDb15.getA825());
        doc.put("a826", byDb15.getA826());
        doc.put("a835", byDb15.getA835());
        doc.put("a850", byDb15.getA850());
        doc.put("a893", byDb15.getA893());
        doc.put("a894", byDb15.getA894());
        doc.put("a895", byDb15.getA895());
        doc.put("table_name", byDb15.getTableName());
        doc.put("filePath", byDb15.getFilePath());
        esContext.modifyDocumentById(CommonConstants.INDEX_PREFIX,byDb15.getId(),doc);
        //跟新数据库
        String dbName = byDb15.getTableName();
        if(StringUtils.isNotEmpty(dbName)){

        }else{
            dbName = byDb15.getA850();
        }
//        iByDb15Service.updateByDb15ByCommonTableName("standard_"+dbName,byDb15);
        iByDb15Service.updateByDb15ByCommonTableName(dbName,byDb15);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult initAllFile() {
        log.info("初始化所有文件开始");
        List<String> tableNameList = CommonConstants.fileTableListBak;
        try {
            for(String tName : tableNameList){
                String taName = "standard_"+tName;
                List<ByDb15> list =  iByDb15Service.selectByCommonList(taName);
                log.info("初始化全部文件{}-----------{}",taName,list.size());
                list.stream().forEach(element -> {
                    try{
                        //获取对应路径文件
                        String a893 = element.getA893();
                        if(StringUtils.isNotNull(a893) && StringUtils.isNotEmpty(a893) && a893.length()>34){
                            //有路径
                            //分割路径
                            //用/进行切割
                            String[] spiltFileName=a893.split("\\\\");
                            //获取最后一个文件名
                            String lastName=spiltFileName[spiltFileName.length-1];
                            //history判断是否存在
                            ByHistoryFile file = iByHistoryFileService.selectByHistoryFileByFileName(lastName);
                            if(file!=null){
                                File pdfFile = new File(file.getFilepath());
                                if (pdfFile.exists()) {
                                    ByDb15 byDb151temp = getDetail(element.getId());
                                    if(byDb151temp.getFilePath().startsWith("http")){

                                    }else{
                                        log.info("{}------------------{}",file.getFilepath(),spiltFileName);
                                        //上传至minio
                                        String[] result = new String[0];
                                        try {
                                            result = iStandardFileService.uploadFile(new MockMultipartFile(pdfFile.getName(), pdfFile.getName(), ContentType.APPLICATION_OCTET_STREAM.toString(), new FileInputStream(pdfFile)));
                                        } catch (Exception e) {
                                            log.error("文件上传---------{}",e.getMessage());
                                            e.printStackTrace();
                                        }
                                        //更新对应a894字段
                                        String realPath = result[0];
                                        //数据库更新
                                        iByDb15Service.updateCommonFileByName(taName,element.getId(),realPath);
                                        //es更新
                                        updateFilePathById(element.getId(),realPath);
                                    }
                                }
                            }
                        }
                    }catch (Exception e){
                        log.info("文件同步循环异常--------{}----------{}",e.getMessage(),element.getId());
                    }
                });
            }
        }catch (Exception e){
            log.error("文件同步异常----{}", e.getMessage());

        }
        return null;
    }

    @Override
    public AjaxResult initTableNameFile(String tableName) {
        log.info("初始化"+tableName+"所有文件开始");
        try {
                String taName = "standard_"+tableName;
                List<ByDb15> list =  iByDb15Service.selectByCommonList(taName);
                log.info("初始化全部文件{}-----------{}",taName,list.size());
                for(ByDb15 byDb15 : list){
                    //获取对应路径文件
                    String a893 = byDb15.getA894();
                    if(StringUtils.isNotNull(a893) && StringUtils.isNotEmpty(a893) && a893.length()>34){
                        //有路径
                        //分割路径
                        //用/进行切割
                        String[] spiltFileName=a893.split("\\\\");
                        //获取最后一个文件名
                        String lastName=spiltFileName[spiltFileName.length-1];
                        //history判断是否存在
                        ByHistoryFile file = iByHistoryFileService.selectByHistoryFileByFileName(lastName);
                        if(file!=null){
                            File pdfFile = new File(file.getFilepath());
                            if (pdfFile.exists()) {
                                ByDb15 byDb151temp = getDetail(byDb15.getId());
                                if(byDb151temp.getFilePath().startsWith("http")){

                                }else{
                                    log.info("{}------------------{}",file.getFilepath(),spiltFileName);
                                    //上传至minio
                                    String[] result = iStandardFileService.uploadFile(new MockMultipartFile(pdfFile.getName(), pdfFile.getName(), ContentType.APPLICATION_OCTET_STREAM.toString(), new FileInputStream(pdfFile)));
                                    //更新对应a894字段
                                    String realPath = result[0];
                                    //数据库更新
                                    iByDb15Service.updateCommonFileByName(taName,byDb15.getId(),realPath);
                                    //es更新
                                    updateFilePathById(byDb15.getId(),realPath);
                                }
                            }
                        }
                    }
                }
        }catch (Exception e){
            log.error("文件同步异常----{}", e.getMessage());
        }
        return null;
    }

    @Override
    public ByDb15 updateById(String id) {
        Map<String,Object> map = esContext.searchIndexDoc(CommonConstants.INDEX_PREFIX,id);
        map.put("a894","");
        esContext.modifyDocumentById(CommonConstants.INDEX_PREFIX,id,map);
        return null;
    }

    @Override
    public int updateFilePathById(String id, String realPath) {
        Map<String,Object> map = esContext.searchIndexDoc(CommonConstants.INDEX_PREFIX,id);
        map.put("filePath",realPath);
        esContext.modifyDocumentById(CommonConstants.INDEX_PREFIX,id,map);
        return 1;
    }

    @Override
    public ByDb15 updateFIlePath(String tableName) {
        try{
            List<ByDb15> list =  iByDb15Service.selectByCommonList(tableName);
            for(ByDb15 byDb15 : list){
                //获取对应路径文件
                String a893 = byDb15.getA894();
                if(StringUtils.isNotNull(a893) && StringUtils.isNotEmpty(a893) && a893.length()>34){
                    //分割路径
                    //用/进行切割
                    String[] spiltFileName=a893.split("\\\\");
                    //获取最后一个文件名
                    String lastName=spiltFileName[spiltFileName.length-1];
                    String filePath = newFilePath+lastName;
                    File pdfFile = new File(filePath);
                    if (pdfFile.exists()) {
                        if(byDb15.getFilePath().startsWith("http")){

                        }else{
                            //上传至minio
                            String[] result = iStandardFileService.uploadFile(new MockMultipartFile(pdfFile.getName(), pdfFile.getName(), ContentType.APPLICATION_OCTET_STREAM.toString(), new FileInputStream(pdfFile)));
                            //更新对应a894字段
                            String realPath = result[0];
                            //数据库更新
                            iByDb15Service.updateCommonFileByName(tableName,byDb15.getId(),realPath);
                            //es更新
                            updateFilePathById(byDb15.getId(),realPath);
                        }
                    }
                }
            }
        }catch (Exception e){
            log.error(e.getMessage());
        }
        return null;
    }


    @Override
    public ByDb15 updateFIlePathByTableName() {
        List<String> tableNameList = CommonConstants.fileTableListBak;
        try {
            for(String tName : tableNameList) {
                List<ByDb15> list =  iByDb15Service.selectByCommonList("standard_" + tName);
                for(ByDb15 byDb15 : list){
                    //获取对应路径文件
                    String standardFile = "";
                    String a893 = byDb15.getA895();
                    String a894 = byDb15.getA894();
                    String a895 = byDb15.getA895();

                    if(StringUtils.isNotEmpty(a893) && a893.length()>34){
                        if(byDb15.getFilePath().startsWith("http")){

                        }else{
                            standardFile = a893;
                        }
                    }

                    if(StringUtils.isNotEmpty(a894) && a894.length()>34){
                        if(byDb15.getFilePath().startsWith("http")){

                        }else{
                            standardFile = a894;
                        }
                    }

                    if(StringUtils.isNotEmpty(a895) && a895.length()>34){
                        if(byDb15.getFilePath().startsWith("http")){

                        }else{
                            standardFile = a895;
                        }
                    }

                    if(StringUtils.isNotEmpty(standardFile) && standardFile.length()>34){
                        //分割路径
                        //用/进行切割
                        String[] spiltFileName=standardFile.split("\\\\");
                        //获取最后一个文件名
                        String lastName=spiltFileName[spiltFileName.length-1];
                        String filePath = newFilePath+lastName;
                        File pdfFile = new File(filePath);
                        if (pdfFile.exists()) {
                            if(byDb15.getFilePath().startsWith("http")){

                            }else{
                                //上传至minio
                                String[] result = iStandardFileService.uploadFile(new MockMultipartFile(pdfFile.getName(), pdfFile.getName(), ContentType.APPLICATION_OCTET_STREAM.toString(), new FileInputStream(pdfFile)));
                                //更新对应a894字段
                                String realPath = result[0];
                                //数据库更新
                                iByDb15Service.updateCommonFileByName("standard_" + tName,byDb15.getId(),realPath);
                                //es更新
                                updateFilePathById(byDb15.getId(),realPath);
                            }
                        }
                    }
                }
            }
            System.out.println("文件同步完成----------------");
        }catch (Exception e){
            log.error(e.getMessage());
        }
        return null;
    }



    @Override
    public AjaxResult totalTitle() {
        ESBuilder b = ESBuilder.builder().build();
        List<String> indexList = new ArrayList<>();
        indexList.add(CommonConstants.INDEX_PREFIX);
        SearchResponse<Object> response = esContext.searchIndex(indexList, b, 1, 1, null, null);
        return AjaxResult.success(response == null ? 0 : response.hits().total().value());
    }

    @Override
    public AjaxResult gbTitle() {
        ESBuilder b = ESBuilder.builder().build();
        ESBuilder build = ESBuilder.builder().build();
        build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.gbString);
        b.buildBoolMust(build.getQuery());

//        b.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.gbString);
        List<String> indexList = new ArrayList<>();
        indexList.add(CommonConstants.INDEX_PREFIX);
        SearchResponse<Object> response = esContext.searchIndex(indexList, b, 1, 1, null, null);
        return AjaxResult.success(response == null ? 0 : response.hits().total().value());
    }

    @Override
    public AjaxResult dbTitle() {
        ESBuilder b = ESBuilder.builder().build();
        ESBuilder build = ESBuilder.builder().build();
        build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.dbString);
        b.buildBoolMust(build.getQuery());
        List<String> indexList = new ArrayList<>();
        indexList.add(CommonConstants.INDEX_PREFIX);
        SearchResponse<Object> response = esContext.searchIndex(indexList, b, 1, 1, null, null);
        return AjaxResult.success(response == null ? 0 : response.hits().total().value());
    }

    @Override
    public AjaxResult hbTitle() {
        ESBuilder b = ESBuilder.builder().build();
        ESBuilder build = ESBuilder.builder().build();
        build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.hbString);
        b.buildBoolMust(build.getQuery());
        List<String> indexList = new ArrayList<>();
        indexList.add(CommonConstants.INDEX_PREFIX);
        SearchResponse<Object> response = esContext.searchIndex(indexList, b, 1, 1, null, null);
        return AjaxResult.success(response == null ? 0 : response.hits().total().value());
    }

    @Override
    public AjaxResult tbTitle() {
        ESBuilder b = ESBuilder.builder().build();
        ESBuilder build = ESBuilder.builder().build();
        build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.tbString);
        b.buildBoolMust(build.getQuery());
        List<String> indexList = new ArrayList<>();
        indexList.add(CommonConstants.INDEX_PREFIX);
        SearchResponse<Object> response = esContext.searchIndex(indexList, b, 1, 1, null, null);
        return AjaxResult.success(response == null ? 0 : response.hits().total().value());
    }

    @Override
    public AjaxResult gwTitle() {
        ESBuilder b = ESBuilder.builder().build();
        ESBuilder build = ESBuilder.builder().build();
        build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.gwString);
        b.buildBoolMust(build.getQuery());
        List<String> indexList = new ArrayList<>();
        indexList.add(CommonConstants.INDEX_PREFIX);
        SearchResponse<Object> response = esContext.searchIndex(indexList, b, 1, 1, null, null);
        return AjaxResult.success(response == null ? 0 : response.hits().total().value());
    }

    @Override
    public AjaxResult gbText() {
        ESBuilder b = ESBuilder.builder().build();
        ESBuilder build = ESBuilder.builder().build();
        build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.gbString);
        b.buildBoolMust(build.getQuery());
        // 新增条件：filePath字段不为空
        b.buildBoolMust(CommonConstants.searchFieldFilePathString,"http");
        List<String> indexList = new ArrayList<>();
        indexList.add(CommonConstants.INDEX_PREFIX);
        SearchResponse<Object> response = esContext.searchIndex(indexList, b, 1, 1, null, null);
//        log.info();
        return AjaxResult.success(response == null ? 0 : response.hits().total().value());
    }

    @Override
    public AjaxResult gwText() {
        ESBuilder b = ESBuilder.builder().build();
        ESBuilder build = ESBuilder.builder().build();
        build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.gwString);
        b.buildBoolMust(build.getQuery());
        // 新增条件：filePath字段不为空
        b.buildBoolMust(CommonConstants.searchFieldFilePathString,"http");
        List<String> indexList = new ArrayList<>();
        indexList.add(CommonConstants.INDEX_PREFIX);
        SearchResponse<Object> response = esContext.searchIndex(indexList, b, 1, 1, null, null);
//        log.info();
        return AjaxResult.success(response == null ? 0 : response.hits().total().value());
    }

    @Override
    public AjaxResult hbText() {
        ESBuilder b = ESBuilder.builder().build();
        ESBuilder build = ESBuilder.builder().build();
        build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.hbString);
        b.buildBoolMust(build.getQuery());
        // 新增条件：filePath字段不为空
        b.buildBoolMust(CommonConstants.searchFieldFilePathString,"http");
        List<String> indexList = new ArrayList<>();
        indexList.add(CommonConstants.INDEX_PREFIX);
        SearchResponse<Object> response = esContext.searchIndex(indexList, b, 1, 1, null, null);
        return AjaxResult.success(response == null ? 0 : response.hits().total().value());
    }

    @Override
    public AjaxResult dbText() {
        ESBuilder b = ESBuilder.builder().build();
        ESBuilder build = ESBuilder.builder().build();
        build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.dbString);
        b.buildBoolMust(build.getQuery());
        // 新增条件：filePath字段不为空
        b.buildBoolMust(CommonConstants.searchFieldFilePathString,"http");
        List<String> indexList = new ArrayList<>();
        indexList.add(CommonConstants.INDEX_PREFIX);
        SearchResponse<Object> response = esContext.searchIndex(indexList, b, 1, 1, null, null);
        return AjaxResult.success(response == null ? 0 : response.hits().total().value());
    }

    @Override
    public AjaxResult tbText() {
        ESBuilder b = ESBuilder.builder().build();
        ESBuilder build = ESBuilder.builder().build();
        build.buildQueryMutiMath850(CommonConstants.searchFieldA850, CommonConstants.tbString);
        b.buildBoolMust(build.getQuery());
        // 新增条件：filePath字段不为空
        b.buildBoolMust(CommonConstants.searchFieldFilePathString,"http");
        List<String> indexList = new ArrayList<>();
        indexList.add(CommonConstants.INDEX_PREFIX);
        SearchResponse<Object> response = esContext.searchIndex(indexList, b, 1, 1, null, null);
        return AjaxResult.success(response == null ? 0 : response.hits().total().value());
    }

    @Override
    public AjaxResult nmTitle() {
        ESBuilder b = ESBuilder.builder().build();
        b.buildBoolMust(CommonConstants.searchFieldA850String, "DB15");
        List<String> indexList = new ArrayList<>();
        indexList.add(CommonConstants.INDEX_PREFIX);
        SearchResponse<Object> response = esContext.searchIndex(indexList, b, 1, 1, null, null);
        return AjaxResult.success(response == null ? 0 : response.hits().total().value());
    }

    //蒙字标
    @Override
    public AjaxResult mTitle() {
        ESBuilder b = ESBuilder.builder().build();
        b.buildBoolMust(CommonConstants.searchFieldA850String, "mzbp");
        List<String> indexList = new ArrayList<>();
        indexList.add(CommonConstants.INDEX_PREFIX);
        SearchResponse<Object> response = esContext.searchIndex(indexList, b, 1, 1, null, null);
        return AjaxResult.success(response == null ? 0 : response.hits().total().value());
    }

    @Override
    public AjaxResult totalFile() {
        ESBuilder b = ESBuilder.builder().build();
        b.buildBoolMust(CommonConstants.searchFieldFilePathString,"http");
        List<String> indexList = new ArrayList<>();
        indexList.add(CommonConstants.INDEX_PREFIX);
        SearchResponse<Object> response = esContext.searchIndex(indexList, b, 1, 1, null, null);
        return AjaxResult.success(response == null ? 0 : response.hits().total().value());
    }

    @Override
    public AjaxResult updateByTableName(String tableName) {
        List<ByDb15> list =  iByDb15Service.selectByCommonList("standard_" + tableName);
        for(ByDb15 byDb15 : list){
            if(StringUtils.isNotEmpty(byDb15.getFilePath()) && byDb15.getFilePath().startsWith("http")){
                String id = byDb15.getId();
                Map<String,Object> map = esContext.getDocument(CommonConstants.INDEX_PREFIX,id,Map.class);
                map.put("filePath",byDb15.getFilePath());
                esContext.modifyDocumentById(CommonConstants.INDEX_PREFIX,id,map);
                log.info("文本es更新---------------{}",byDb15.getA100());
            }
        }
        return AjaxResult.success();
    }
}
